file_name
stringlengths 5
52
| name
stringlengths 4
95
| original_source_type
stringlengths 0
23k
| source_type
stringlengths 9
23k
| source_definition
stringlengths 9
57.9k
| source
dict | source_range
dict | file_context
stringlengths 0
721k
| dependencies
dict | opens_and_abbrevs
listlengths 2
94
| vconfig
dict | interleaved
bool 1
class | verbose_type
stringlengths 1
7.42k
| effect
stringclasses 118
values | effect_flags
sequencelengths 0
2
| mutual_with
sequencelengths 0
11
| ideal_premises
sequencelengths 0
236
| proof_features
sequencelengths 0
1
| is_simple_lemma
bool 2
classes | is_div
bool 2
classes | is_proof
bool 2
classes | is_simply_typed
bool 2
classes | is_type
bool 2
classes | partial_definition
stringlengths 5
3.99k
| completed_definiton
stringlengths 1
1.63M
| isa_cross_project_example
bool 1
class |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
LowParse.Low.DepLen.fst | LowParse.Low.DepLen.validate_deplen | val validate_deplen
(min: nat)
(max: nat{min <= max /\ max < 4294967296})
(#hk: parser_kind)
(#ht: Type)
(#hp: parser hk ht)
(hv: validator hp)
(dlf: (ht -> Tot (bounded_int32 min max)))
(dlfc: deplen_func min max hp dlf)
(#pk: parser_kind)
(#pt: Type)
(#pp: parser pk pt)
(ps: serializer pp)
(pv: validator pp)
: Tot (validator (parse_deplen min max hp dlf ps)) | val validate_deplen
(min: nat)
(max: nat{min <= max /\ max < 4294967296})
(#hk: parser_kind)
(#ht: Type)
(#hp: parser hk ht)
(hv: validator hp)
(dlf: (ht -> Tot (bounded_int32 min max)))
(dlfc: deplen_func min max hp dlf)
(#pk: parser_kind)
(#pt: Type)
(#pp: parser pk pt)
(ps: serializer pp)
(pv: validator pp)
: Tot (validator (parse_deplen min max hp dlf ps)) | let validate_deplen
(min: nat)
(max: nat { min <= max /\ max < 4294967296 })
(#hk: parser_kind)
(#ht: Type)
(#hp: parser hk ht)
(hv: validator hp)
(dlf: ht -> Tot (bounded_int32 min max))
(dlfc: deplen_func min max hp dlf)
(#pk: parser_kind)
(#pt: Type)
(#pp: parser pk pt)
(ps: serializer pp)
(pv: validator pp)
: Tot (validator (parse_deplen min max hp dlf ps))
= fun #rrel #rel (input: slice rrel rel) pos ->
let h = HST.get () in
let _ =
valid_deplen min max hp dlf ps input (uint64_to_uint32 pos) h
in
let pos_payload = hv input pos in
if is_error pos_payload then
pos_payload
else
let payload_len = dlfc input (uint64_to_uint32 pos) in
if (Cast.uint32_to_uint64 input.len `U64.sub` pos_payload) `U64.lt` Cast.uint32_to_uint64 payload_len then
validator_error_not_enough_data
else
let pos_end = uint64_to_uint32 pos_payload `U32.add` payload_len in
let _ = valid_exact_valid_pos_equiv pp h input (uint64_to_uint32 pos_payload) pos_end in
[@inline_let] let input' = { base = input.base; len = pos_end; } in
let pos_end' = pv input' pos_payload in
if is_error pos_end' then
pos_end'
else
if uint64_to_uint32 pos_end' = pos_end then
let _ = valid_deplen_len min max hp dlf ps input (uint64_to_uint32 pos) h in
pos_end'
else
validator_error_generic | {
"file_name": "src/lowparse/LowParse.Low.DepLen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 33,
"end_line": 202,
"start_col": 0,
"start_line": 163
} | module LowParse.Low.DepLen
include LowParse.Spec.DepLen
include LowParse.Low.Base
include LowParse.Low.Combinators
module B = LowStar.Monotonic.Buffer
module U32 = FStar.UInt32
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module Cast = FStar.Int.Cast
module U64 = FStar.UInt64
let valid_exact_valid_pos_equiv
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(h: HS.mem)
#rrel #rel
(input: slice rrel rel)
(pos_begin: U32.t)
(pos_end: U32.t)
: Lemma
(requires live_slice h input)
(ensures
((valid_exact p h input pos_begin pos_end)
<==>
(U32.v pos_begin <= U32.v pos_end /\
U32.v pos_end <= U32.v input.len /\
(let input' = { base = input.base; len = pos_end; } in
valid_pos p h input' pos_begin pos_end))))
= valid_exact_equiv p h input pos_begin pos_end;
if U32.v pos_begin <= U32.v pos_end && U32.v pos_end <= U32.v input.len then
begin
let input' = { base = input.base; len = pos_end; } in
valid_facts p h input' pos_begin
end
(* the validity lemma says it is equivalent to have a valid header followed by a valid payload and
have a valid piece of deplen data *)
let valid_deplen_decomp
(min: nat)
(max: nat { min <= max /\ max < 4294967296 } )
(#hk: parser_kind)
(#ht: Type)
(hp: parser hk ht)
(dlf: ht -> Tot (bounded_int32 min max) )
(#pk: parser_kind)
(#pt: Type)
(pp: parser pk pt)
#rrel #rel
(input: slice rrel rel)
(pos: U32.t)
(h: HS.mem)
= valid hp h input pos /\
(let pos_payload = get_valid_pos hp h input pos in
let len = dlf (contents hp h input pos) in
U32.v pos_payload + U32.v len <= U32.v input.len /\
valid_exact pp h input pos_payload (pos_payload `U32.add` len))
let valid_deplen
(min: nat)
(max: nat { min <= max /\ max < 4294967296 } )
(#hk: parser_kind)
(#ht: Type)
(hp: parser hk ht)
(dlf: ht -> Tot (bounded_int32 min max) )
(#pk: parser_kind)
(#pt: Type)
(#pp: parser pk pt)
(ps: serializer pp)
#rrel #rel
(input: slice rrel rel)
(pos: U32.t)
(h: HS.mem)
: Lemma
(requires live_slice h input)
(ensures
((valid (parse_deplen min max hp dlf ps) h input pos)
<==>
(valid_deplen_decomp min max hp dlf pp input pos h)))
= valid_facts (parse_deplen min max hp dlf ps) h input pos;
valid_facts hp h input pos;
parse_deplen_unfold2 min max hp dlf ps (bytes_of_slice_from h input pos);
if valid_dec hp h input pos then
begin
let pos_payload = get_valid_pos hp h input pos in
let len = dlf (contents hp h input pos) in
if U32.v pos_payload + U32.v len <= U32.v input.len then
begin
let pos_end = pos_payload `U32.add` len in
valid_exact_equiv pp h input pos_payload pos_end;
match parse pp (bytes_of_slice_from_to h input pos_payload pos_end) with
| None -> ()
| Some (_, consumed) -> if consumed = U32.v len then
valid_exact_serialize ps h input pos_payload pos_end
end
end
let valid_deplen_len
(min: nat)
(max: nat { min <= max /\ max < 4294967296 } )
(#hk: parser_kind)
(#ht: Type)
(hp: parser hk ht)
(dlf: ht -> Tot (bounded_int32 min max) )
(#pk: parser_kind)
(#pt: Type)
(#pp: parser pk pt)
(ps: serializer pp)
#rrel #rel
(input: slice rrel rel)
(pos: U32.t)
(h: HS.mem)
: Lemma
(requires (valid (parse_deplen min max hp dlf ps) h input pos) /\
(valid_deplen_decomp min max hp dlf pp input pos h))
(ensures (let pos_payload = get_valid_pos hp h input pos in
let len_payload = dlf (contents hp h input pos) in
let input' = { base = input.base; len = (pos_payload `U32.add` len_payload); } in
valid pp h input' pos_payload /\
(let pos_end' = get_valid_pos pp h input' pos_payload in
let pos_end = get_valid_pos (parse_deplen min max hp dlf ps) h input pos in
pos_end == pos_end')))
= valid_facts (parse_deplen min max hp dlf ps) h input pos;
valid_facts hp h input pos;
parse_deplen_unfold2 min max hp dlf ps (bytes_of_slice_from h input pos);
if valid_dec hp h input pos then
begin
let pos_payload = get_valid_pos hp h input pos in
let len = dlf (contents hp h input pos) in
if U32.v pos_payload + U32.v len <= U32.v input.len then
begin
let pos_end = pos_payload `U32.add` len in
valid_exact_equiv pp h input pos_payload pos_end;
valid_exact_valid_pos_equiv pp h input pos_payload pos_end;
let input' = { base = input.base; len = pos_end; } in
valid_pos_get_valid_pos pp h input' pos_payload pos_end
end
end
inline_for_extraction
let deplen_func
(min: nat)
(max: nat { min <= max /\ max < 4294967296 } )
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(dlf: t -> Tot (bounded_int32 min max) )
: Tot Type
= (#rrel: _) -> (#rel: _) ->
(input: slice rrel rel) ->
(pos: U32.t) ->
HST.Stack (bounded_int32 min max)
(requires (fun h -> valid p h input pos))
(ensures (fun h res h' -> res == dlf (contents p h input pos) /\ B.modifies B.loc_none h h'))
(* the validator for deplen data first validates the header then the payload *) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowParse.Spec.DepLen.fst.checked",
"LowParse.Low.Combinators.fsti.checked",
"LowParse.Low.Base.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Int.Cast.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowParse.Low.DepLen.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.Int.Cast",
"short_module": "Cast"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "LowParse.Low.Combinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Low.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.DepLen",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Low",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Low",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max < 4294967296} ->
hv: LowParse.Low.Base.validator hp ->
dlf: (_: ht -> LowParse.Spec.BoundedInt.bounded_int32 min max) ->
dlfc: LowParse.Low.DepLen.deplen_func min max hp dlf ->
ps: LowParse.Spec.Base.serializer pp ->
pv: LowParse.Low.Base.validator pp
-> LowParse.Low.Base.validator (LowParse.Spec.DepLen.parse_deplen min max hp dlf ps) | Prims.Tot | [
"total"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_LessThan",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Low.Base.validator",
"LowParse.Spec.BoundedInt.bounded_int32",
"LowParse.Low.DepLen.deplen_func",
"LowParse.Spec.Base.serializer",
"LowParse.Slice.srel",
"LowParse.Bytes.byte",
"LowParse.Slice.slice",
"FStar.UInt64.t",
"LowParse.Low.ErrorCode.is_error",
"Prims.bool",
"FStar.UInt64.lt",
"FStar.UInt64.sub",
"FStar.Int.Cast.uint32_to_uint64",
"LowParse.Slice.__proj__Mkslice__item__len",
"LowParse.Low.ErrorCode.validator_error_not_enough_data",
"Prims.op_Equality",
"FStar.UInt32.t",
"LowParse.Low.ErrorCode.uint64_to_uint32",
"Prims.unit",
"LowParse.Low.DepLen.valid_deplen_len",
"LowParse.Low.ErrorCode.validator_error_generic",
"LowParse.Slice.Mkslice",
"LowParse.Slice.__proj__Mkslice__item__base",
"LowParse.Low.DepLen.valid_exact_valid_pos_equiv",
"FStar.UInt32.add",
"LowParse.Low.DepLen.valid_deplen",
"FStar.Monotonic.HyperStack.mem",
"FStar.HyperStack.ST.get",
"LowParse.Spec.DepLen.parse_deplen_kind",
"LowParse.Spec.DepLen.parse_deplen_data_t",
"LowParse.Spec.DepLen.parse_deplen"
] | [] | false | false | false | false | false | let validate_deplen
(min: nat)
(max: nat{min <= max /\ max < 4294967296})
(#hk: parser_kind)
(#ht: Type)
(#hp: parser hk ht)
(hv: validator hp)
(dlf: (ht -> Tot (bounded_int32 min max)))
(dlfc: deplen_func min max hp dlf)
(#pk: parser_kind)
(#pt: Type)
(#pp: parser pk pt)
(ps: serializer pp)
(pv: validator pp)
: Tot (validator (parse_deplen min max hp dlf ps)) =
| fun #rrel #rel (input: slice rrel rel) pos ->
let h = HST.get () in
let _ = valid_deplen min max hp dlf ps input (uint64_to_uint32 pos) h in
let pos_payload = hv input pos in
if is_error pos_payload
then pos_payload
else
let payload_len = dlfc input (uint64_to_uint32 pos) in
if
((Cast.uint32_to_uint64 input.len) `U64.sub` pos_payload)
`U64.lt`
(Cast.uint32_to_uint64 payload_len)
then validator_error_not_enough_data
else
let pos_end = (uint64_to_uint32 pos_payload) `U32.add` payload_len in
let _ = valid_exact_valid_pos_equiv pp h input (uint64_to_uint32 pos_payload) pos_end in
[@@ inline_let ]let input' = { base = input.base; len = pos_end } in
let pos_end' = pv input' pos_payload in
if is_error pos_end'
then pos_end'
else
if uint64_to_uint32 pos_end' = pos_end
then
let _ = valid_deplen_len min max hp dlf ps input (uint64_to_uint32 pos) h in
pos_end'
else validator_error_generic | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.normal | val normal : x: a -> a | let normal (#a:Type) (x:a) = norm normal_steps x | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 48,
"end_line": 120,
"start_col": 0,
"start_line": 120
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver. | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: a -> a | Prims.Tot | [
"total"
] | [] | [
"FStar.Pervasives.norm",
"Steel.Effect.Common.normal_steps"
] | [] | false | false | false | true | false | let normal (#a: Type) (x: a) =
| norm normal_steps x | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.normal_steps | val normal_steps : Prims.list FStar.Pervasives.norm_step | let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify] | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 32,
"end_line": 115,
"start_col": 0,
"start_line": 109
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.list FStar.Pervasives.norm_step | Prims.Tot | [
"total"
] | [] | [
"Prims.Cons",
"FStar.Pervasives.norm_step",
"FStar.Pervasives.delta_attr",
"Prims.string",
"Prims.Nil",
"FStar.Pervasives.delta_only",
"FStar.Pervasives.delta_qualifier",
"FStar.Pervasives.iota",
"FStar.Pervasives.zeta",
"FStar.Pervasives.primops",
"FStar.Pervasives.simplify"
] | [] | false | false | false | true | false | let normal_steps =
| [
delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [
`%Mkvprop'?.t;
`%Mkvprop'?.hp;
`%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit
];
delta_qualifier ["unfold"];
iota;
zeta;
primops;
simplify
] | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.star | val star : _0: Steel.Effect.Common.vprop -> _1: Steel.Effect.Common.vprop -> Steel.Effect.Common.vprop | let star = VStar | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 16,
"end_line": 124,
"start_col": 0,
"start_line": 124
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _0: Steel.Effect.Common.vprop -> _1: Steel.Effect.Common.vprop -> Steel.Effect.Common.vprop | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.VStar"
] | [] | false | false | false | true | false | let star =
| VStar | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.to_vprop | val to_vprop : p: Steel.Memory.slprop -> Steel.Effect.Common.vprop | let to_vprop (p:slprop) = VUnit (to_vprop' p) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 45,
"end_line": 103,
"start_col": 0,
"start_line": 103
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__] | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Steel.Memory.slprop -> Steel.Effect.Common.vprop | Prims.Tot | [
"total"
] | [] | [
"Steel.Memory.slprop",
"Steel.Effect.Common.VUnit",
"Steel.Effect.Common.to_vprop'",
"Steel.Effect.Common.vprop"
] | [] | false | false | false | true | false | let to_vprop (p: slprop) =
| VUnit (to_vprop' p) | false |
|
LowParse.Low.DepLen.fst | LowParse.Low.DepLen.valid_deplen | val valid_deplen
(min: nat)
(max: nat{min <= max /\ max < 4294967296})
(#hk: parser_kind)
(#ht: Type)
(hp: parser hk ht)
(dlf: (ht -> Tot (bounded_int32 min max)))
(#pk: parser_kind)
(#pt: Type)
(#pp: parser pk pt)
(ps: serializer pp)
(#rrel #rel: _)
(input: slice rrel rel)
(pos: U32.t)
(h: HS.mem)
: Lemma (requires live_slice h input)
(ensures
((valid (parse_deplen min max hp dlf ps) h input pos) <==>
(valid_deplen_decomp min max hp dlf pp input pos h))) | val valid_deplen
(min: nat)
(max: nat{min <= max /\ max < 4294967296})
(#hk: parser_kind)
(#ht: Type)
(hp: parser hk ht)
(dlf: (ht -> Tot (bounded_int32 min max)))
(#pk: parser_kind)
(#pt: Type)
(#pp: parser pk pt)
(ps: serializer pp)
(#rrel #rel: _)
(input: slice rrel rel)
(pos: U32.t)
(h: HS.mem)
: Lemma (requires live_slice h input)
(ensures
((valid (parse_deplen min max hp dlf ps) h input pos) <==>
(valid_deplen_decomp min max hp dlf pp input pos h))) | let valid_deplen
(min: nat)
(max: nat { min <= max /\ max < 4294967296 } )
(#hk: parser_kind)
(#ht: Type)
(hp: parser hk ht)
(dlf: ht -> Tot (bounded_int32 min max) )
(#pk: parser_kind)
(#pt: Type)
(#pp: parser pk pt)
(ps: serializer pp)
#rrel #rel
(input: slice rrel rel)
(pos: U32.t)
(h: HS.mem)
: Lemma
(requires live_slice h input)
(ensures
((valid (parse_deplen min max hp dlf ps) h input pos)
<==>
(valid_deplen_decomp min max hp dlf pp input pos h)))
= valid_facts (parse_deplen min max hp dlf ps) h input pos;
valid_facts hp h input pos;
parse_deplen_unfold2 min max hp dlf ps (bytes_of_slice_from h input pos);
if valid_dec hp h input pos then
begin
let pos_payload = get_valid_pos hp h input pos in
let len = dlf (contents hp h input pos) in
if U32.v pos_payload + U32.v len <= U32.v input.len then
begin
let pos_end = pos_payload `U32.add` len in
valid_exact_equiv pp h input pos_payload pos_end;
match parse pp (bytes_of_slice_from_to h input pos_payload pos_end) with
| None -> ()
| Some (_, consumed) -> if consumed = U32.v len then
valid_exact_serialize ps h input pos_payload pos_end
end
end | {
"file_name": "src/lowparse/LowParse.Low.DepLen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 5,
"end_line": 99,
"start_col": 0,
"start_line": 62
} | module LowParse.Low.DepLen
include LowParse.Spec.DepLen
include LowParse.Low.Base
include LowParse.Low.Combinators
module B = LowStar.Monotonic.Buffer
module U32 = FStar.UInt32
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module Cast = FStar.Int.Cast
module U64 = FStar.UInt64
let valid_exact_valid_pos_equiv
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(h: HS.mem)
#rrel #rel
(input: slice rrel rel)
(pos_begin: U32.t)
(pos_end: U32.t)
: Lemma
(requires live_slice h input)
(ensures
((valid_exact p h input pos_begin pos_end)
<==>
(U32.v pos_begin <= U32.v pos_end /\
U32.v pos_end <= U32.v input.len /\
(let input' = { base = input.base; len = pos_end; } in
valid_pos p h input' pos_begin pos_end))))
= valid_exact_equiv p h input pos_begin pos_end;
if U32.v pos_begin <= U32.v pos_end && U32.v pos_end <= U32.v input.len then
begin
let input' = { base = input.base; len = pos_end; } in
valid_facts p h input' pos_begin
end
(* the validity lemma says it is equivalent to have a valid header followed by a valid payload and
have a valid piece of deplen data *)
let valid_deplen_decomp
(min: nat)
(max: nat { min <= max /\ max < 4294967296 } )
(#hk: parser_kind)
(#ht: Type)
(hp: parser hk ht)
(dlf: ht -> Tot (bounded_int32 min max) )
(#pk: parser_kind)
(#pt: Type)
(pp: parser pk pt)
#rrel #rel
(input: slice rrel rel)
(pos: U32.t)
(h: HS.mem)
= valid hp h input pos /\
(let pos_payload = get_valid_pos hp h input pos in
let len = dlf (contents hp h input pos) in
U32.v pos_payload + U32.v len <= U32.v input.len /\
valid_exact pp h input pos_payload (pos_payload `U32.add` len)) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowParse.Spec.DepLen.fst.checked",
"LowParse.Low.Combinators.fsti.checked",
"LowParse.Low.Base.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Int.Cast.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowParse.Low.DepLen.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.Int.Cast",
"short_module": "Cast"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "LowParse.Low.Combinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Low.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.DepLen",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Low",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Low",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max < 4294967296} ->
hp: LowParse.Spec.Base.parser hk ht ->
dlf: (_: ht -> LowParse.Spec.BoundedInt.bounded_int32 min max) ->
ps: LowParse.Spec.Base.serializer pp ->
input: LowParse.Slice.slice rrel rel ->
pos: FStar.UInt32.t ->
h: FStar.Monotonic.HyperStack.mem
-> FStar.Pervasives.Lemma (requires LowParse.Slice.live_slice h input)
(ensures
LowParse.Low.Base.Spec.valid (LowParse.Spec.DepLen.parse_deplen min max hp dlf ps)
h
input
pos <==>
LowParse.Low.DepLen.valid_deplen_decomp min max hp dlf pp input pos h) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_LessThan",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.BoundedInt.bounded_int32",
"LowParse.Spec.Base.serializer",
"LowParse.Slice.srel",
"LowParse.Bytes.byte",
"LowParse.Slice.slice",
"FStar.UInt32.t",
"FStar.Monotonic.HyperStack.mem",
"LowParse.Low.Base.Spec.valid_dec",
"Prims.op_Addition",
"FStar.UInt32.v",
"LowParse.Slice.__proj__Mkslice__item__len",
"LowParse.Spec.Base.parse",
"LowParse.Low.Base.Spec.bytes_of_slice_from_to",
"LowParse.Spec.Base.consumed_length",
"Prims.op_Equality",
"Prims.int",
"Prims.l_or",
"Prims.op_GreaterThanOrEqual",
"FStar.Seq.Base.length",
"FStar.UInt.size",
"FStar.UInt32.n",
"LowParse.Low.Base.Spec.valid_exact_serialize",
"Prims.bool",
"Prims.unit",
"LowParse.Low.Base.Spec.valid_exact_equiv",
"FStar.UInt32.add",
"LowParse.Low.Base.Spec.contents",
"LowParse.Low.Base.Spec.get_valid_pos",
"LowParse.Spec.DepLen.parse_deplen_unfold2",
"LowParse.Slice.bytes_of_slice_from",
"LowParse.Low.Base.Spec.valid_facts",
"LowParse.Spec.DepLen.parse_deplen_kind",
"LowParse.Spec.DepLen.parse_deplen_data_t",
"LowParse.Spec.DepLen.parse_deplen",
"LowParse.Slice.live_slice",
"Prims.squash",
"Prims.l_iff",
"LowParse.Low.Base.Spec.valid",
"LowParse.Low.DepLen.valid_deplen_decomp",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let valid_deplen
(min: nat)
(max: nat{min <= max /\ max < 4294967296})
(#hk: parser_kind)
(#ht: Type)
(hp: parser hk ht)
(dlf: (ht -> Tot (bounded_int32 min max)))
(#pk: parser_kind)
(#pt: Type)
(#pp: parser pk pt)
(ps: serializer pp)
#rrel
#rel
(input: slice rrel rel)
(pos: U32.t)
(h: HS.mem)
: Lemma (requires live_slice h input)
(ensures
((valid (parse_deplen min max hp dlf ps) h input pos) <==>
(valid_deplen_decomp min max hp dlf pp input pos h))) =
| valid_facts (parse_deplen min max hp dlf ps) h input pos;
valid_facts hp h input pos;
parse_deplen_unfold2 min max hp dlf ps (bytes_of_slice_from h input pos);
if valid_dec hp h input pos
then
let pos_payload = get_valid_pos hp h input pos in
let len = dlf (contents hp h input pos) in
if U32.v pos_payload + U32.v len <= U32.v input.len
then
let pos_end = pos_payload `U32.add` len in
valid_exact_equiv pp h input pos_payload pos_end;
match parse pp (bytes_of_slice_from_to h input pos_payload pos_end) with
| None -> ()
| Some (_, consumed) ->
if consumed = U32.v len then valid_exact_serialize ps h input pos_payload pos_end | false |
LowParse.Low.DepLen.fst | LowParse.Low.DepLen.valid_deplen_len | val valid_deplen_len
(min: nat)
(max: nat{min <= max /\ max < 4294967296})
(#hk: parser_kind)
(#ht: Type)
(hp: parser hk ht)
(dlf: (ht -> Tot (bounded_int32 min max)))
(#pk: parser_kind)
(#pt: Type)
(#pp: parser pk pt)
(ps: serializer pp)
(#rrel #rel: _)
(input: slice rrel rel)
(pos: U32.t)
(h: HS.mem)
: Lemma
(requires
(valid (parse_deplen min max hp dlf ps) h input pos) /\
(valid_deplen_decomp min max hp dlf pp input pos h))
(ensures
(let pos_payload = get_valid_pos hp h input pos in
let len_payload = dlf (contents hp h input pos) in
let input' = { base = input.base; len = (pos_payload `U32.add` len_payload) } in
valid pp h input' pos_payload /\
(let pos_end' = get_valid_pos pp h input' pos_payload in
let pos_end = get_valid_pos (parse_deplen min max hp dlf ps) h input pos in
pos_end == pos_end'))) | val valid_deplen_len
(min: nat)
(max: nat{min <= max /\ max < 4294967296})
(#hk: parser_kind)
(#ht: Type)
(hp: parser hk ht)
(dlf: (ht -> Tot (bounded_int32 min max)))
(#pk: parser_kind)
(#pt: Type)
(#pp: parser pk pt)
(ps: serializer pp)
(#rrel #rel: _)
(input: slice rrel rel)
(pos: U32.t)
(h: HS.mem)
: Lemma
(requires
(valid (parse_deplen min max hp dlf ps) h input pos) /\
(valid_deplen_decomp min max hp dlf pp input pos h))
(ensures
(let pos_payload = get_valid_pos hp h input pos in
let len_payload = dlf (contents hp h input pos) in
let input' = { base = input.base; len = (pos_payload `U32.add` len_payload) } in
valid pp h input' pos_payload /\
(let pos_end' = get_valid_pos pp h input' pos_payload in
let pos_end = get_valid_pos (parse_deplen min max hp dlf ps) h input pos in
pos_end == pos_end'))) | let valid_deplen_len
(min: nat)
(max: nat { min <= max /\ max < 4294967296 } )
(#hk: parser_kind)
(#ht: Type)
(hp: parser hk ht)
(dlf: ht -> Tot (bounded_int32 min max) )
(#pk: parser_kind)
(#pt: Type)
(#pp: parser pk pt)
(ps: serializer pp)
#rrel #rel
(input: slice rrel rel)
(pos: U32.t)
(h: HS.mem)
: Lemma
(requires (valid (parse_deplen min max hp dlf ps) h input pos) /\
(valid_deplen_decomp min max hp dlf pp input pos h))
(ensures (let pos_payload = get_valid_pos hp h input pos in
let len_payload = dlf (contents hp h input pos) in
let input' = { base = input.base; len = (pos_payload `U32.add` len_payload); } in
valid pp h input' pos_payload /\
(let pos_end' = get_valid_pos pp h input' pos_payload in
let pos_end = get_valid_pos (parse_deplen min max hp dlf ps) h input pos in
pos_end == pos_end')))
= valid_facts (parse_deplen min max hp dlf ps) h input pos;
valid_facts hp h input pos;
parse_deplen_unfold2 min max hp dlf ps (bytes_of_slice_from h input pos);
if valid_dec hp h input pos then
begin
let pos_payload = get_valid_pos hp h input pos in
let len = dlf (contents hp h input pos) in
if U32.v pos_payload + U32.v len <= U32.v input.len then
begin
let pos_end = pos_payload `U32.add` len in
valid_exact_equiv pp h input pos_payload pos_end;
valid_exact_valid_pos_equiv pp h input pos_payload pos_end;
let input' = { base = input.base; len = pos_end; } in
valid_pos_get_valid_pos pp h input' pos_payload pos_end
end
end | {
"file_name": "src/lowparse/LowParse.Low.DepLen.fst",
"git_rev": "00217c4a89f5ba56002ba9aa5b4a9d5903bfe9fa",
"git_url": "https://github.com/project-everest/everparse.git",
"project_name": "everparse"
} | {
"end_col": 5,
"end_line": 141,
"start_col": 0,
"start_line": 101
} | module LowParse.Low.DepLen
include LowParse.Spec.DepLen
include LowParse.Low.Base
include LowParse.Low.Combinators
module B = LowStar.Monotonic.Buffer
module U32 = FStar.UInt32
module HS = FStar.HyperStack
module HST = FStar.HyperStack.ST
module Cast = FStar.Int.Cast
module U64 = FStar.UInt64
let valid_exact_valid_pos_equiv
(#k: parser_kind)
(#t: Type)
(p: parser k t)
(h: HS.mem)
#rrel #rel
(input: slice rrel rel)
(pos_begin: U32.t)
(pos_end: U32.t)
: Lemma
(requires live_slice h input)
(ensures
((valid_exact p h input pos_begin pos_end)
<==>
(U32.v pos_begin <= U32.v pos_end /\
U32.v pos_end <= U32.v input.len /\
(let input' = { base = input.base; len = pos_end; } in
valid_pos p h input' pos_begin pos_end))))
= valid_exact_equiv p h input pos_begin pos_end;
if U32.v pos_begin <= U32.v pos_end && U32.v pos_end <= U32.v input.len then
begin
let input' = { base = input.base; len = pos_end; } in
valid_facts p h input' pos_begin
end
(* the validity lemma says it is equivalent to have a valid header followed by a valid payload and
have a valid piece of deplen data *)
let valid_deplen_decomp
(min: nat)
(max: nat { min <= max /\ max < 4294967296 } )
(#hk: parser_kind)
(#ht: Type)
(hp: parser hk ht)
(dlf: ht -> Tot (bounded_int32 min max) )
(#pk: parser_kind)
(#pt: Type)
(pp: parser pk pt)
#rrel #rel
(input: slice rrel rel)
(pos: U32.t)
(h: HS.mem)
= valid hp h input pos /\
(let pos_payload = get_valid_pos hp h input pos in
let len = dlf (contents hp h input pos) in
U32.v pos_payload + U32.v len <= U32.v input.len /\
valid_exact pp h input pos_payload (pos_payload `U32.add` len))
let valid_deplen
(min: nat)
(max: nat { min <= max /\ max < 4294967296 } )
(#hk: parser_kind)
(#ht: Type)
(hp: parser hk ht)
(dlf: ht -> Tot (bounded_int32 min max) )
(#pk: parser_kind)
(#pt: Type)
(#pp: parser pk pt)
(ps: serializer pp)
#rrel #rel
(input: slice rrel rel)
(pos: U32.t)
(h: HS.mem)
: Lemma
(requires live_slice h input)
(ensures
((valid (parse_deplen min max hp dlf ps) h input pos)
<==>
(valid_deplen_decomp min max hp dlf pp input pos h)))
= valid_facts (parse_deplen min max hp dlf ps) h input pos;
valid_facts hp h input pos;
parse_deplen_unfold2 min max hp dlf ps (bytes_of_slice_from h input pos);
if valid_dec hp h input pos then
begin
let pos_payload = get_valid_pos hp h input pos in
let len = dlf (contents hp h input pos) in
if U32.v pos_payload + U32.v len <= U32.v input.len then
begin
let pos_end = pos_payload `U32.add` len in
valid_exact_equiv pp h input pos_payload pos_end;
match parse pp (bytes_of_slice_from_to h input pos_payload pos_end) with
| None -> ()
| Some (_, consumed) -> if consumed = U32.v len then
valid_exact_serialize ps h input pos_payload pos_end
end
end | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"LowStar.Monotonic.Buffer.fsti.checked",
"LowParse.Spec.DepLen.fst.checked",
"LowParse.Low.Combinators.fsti.checked",
"LowParse.Low.Base.fst.checked",
"FStar.UInt64.fsti.checked",
"FStar.UInt32.fsti.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Int.Cast.fst.checked",
"FStar.HyperStack.ST.fsti.checked",
"FStar.HyperStack.fst.checked"
],
"interface_file": false,
"source_file": "LowParse.Low.DepLen.fst"
} | [
{
"abbrev": true,
"full_module": "FStar.UInt64",
"short_module": "U64"
},
{
"abbrev": true,
"full_module": "FStar.Int.Cast",
"short_module": "Cast"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack.ST",
"short_module": "HST"
},
{
"abbrev": true,
"full_module": "FStar.HyperStack",
"short_module": "HS"
},
{
"abbrev": true,
"full_module": "FStar.UInt32",
"short_module": "U32"
},
{
"abbrev": true,
"full_module": "LowStar.Monotonic.Buffer",
"short_module": "B"
},
{
"abbrev": false,
"full_module": "LowParse.Low.Combinators",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Low.Base",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Spec.DepLen",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Low",
"short_module": null
},
{
"abbrev": false,
"full_module": "LowParse.Low",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
min: Prims.nat ->
max: Prims.nat{min <= max /\ max < 4294967296} ->
hp: LowParse.Spec.Base.parser hk ht ->
dlf: (_: ht -> LowParse.Spec.BoundedInt.bounded_int32 min max) ->
ps: LowParse.Spec.Base.serializer pp ->
input: LowParse.Slice.slice rrel rel ->
pos: FStar.UInt32.t ->
h: FStar.Monotonic.HyperStack.mem
-> FStar.Pervasives.Lemma
(requires
LowParse.Low.Base.Spec.valid (LowParse.Spec.DepLen.parse_deplen min max hp dlf ps)
h
input
pos /\ LowParse.Low.DepLen.valid_deplen_decomp min max hp dlf pp input pos h)
(ensures
(let pos_payload = LowParse.Low.Base.Spec.get_valid_pos hp h input pos in
let len_payload = dlf (LowParse.Low.Base.Spec.contents hp h input pos) in
let input' =
LowParse.Slice.Mkslice (Mkslice?.base input) (FStar.UInt32.add pos_payload len_payload)
in
LowParse.Low.Base.Spec.valid pp h input' pos_payload /\
(let pos_end' = LowParse.Low.Base.Spec.get_valid_pos pp h input' pos_payload in
let pos_end =
LowParse.Low.Base.Spec.get_valid_pos (LowParse.Spec.DepLen.parse_deplen min
max
hp
dlf
ps)
h
input
pos
in
pos_end == pos_end'))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"Prims.l_and",
"Prims.b2t",
"Prims.op_LessThanOrEqual",
"Prims.op_LessThan",
"LowParse.Spec.Base.parser_kind",
"LowParse.Spec.Base.parser",
"LowParse.Spec.BoundedInt.bounded_int32",
"LowParse.Spec.Base.serializer",
"LowParse.Slice.srel",
"LowParse.Bytes.byte",
"LowParse.Slice.slice",
"FStar.UInt32.t",
"FStar.Monotonic.HyperStack.mem",
"LowParse.Low.Base.Spec.valid_dec",
"Prims.op_Addition",
"FStar.UInt32.v",
"LowParse.Slice.__proj__Mkslice__item__len",
"LowParse.Low.Base.Spec.valid_pos_get_valid_pos",
"LowParse.Slice.Mkslice",
"LowParse.Slice.__proj__Mkslice__item__base",
"Prims.unit",
"LowParse.Low.DepLen.valid_exact_valid_pos_equiv",
"LowParse.Low.Base.Spec.valid_exact_equiv",
"FStar.UInt32.add",
"Prims.bool",
"LowParse.Low.Base.Spec.contents",
"LowParse.Low.Base.Spec.get_valid_pos",
"LowParse.Spec.DepLen.parse_deplen_unfold2",
"LowParse.Slice.bytes_of_slice_from",
"LowParse.Low.Base.Spec.valid_facts",
"LowParse.Spec.DepLen.parse_deplen_kind",
"LowParse.Spec.DepLen.parse_deplen_data_t",
"LowParse.Spec.DepLen.parse_deplen",
"LowParse.Low.Base.Spec.valid",
"LowParse.Low.DepLen.valid_deplen_decomp",
"Prims.squash",
"Prims.eq2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let valid_deplen_len
(min: nat)
(max: nat{min <= max /\ max < 4294967296})
(#hk: parser_kind)
(#ht: Type)
(hp: parser hk ht)
(dlf: (ht -> Tot (bounded_int32 min max)))
(#pk: parser_kind)
(#pt: Type)
(#pp: parser pk pt)
(ps: serializer pp)
#rrel
#rel
(input: slice rrel rel)
(pos: U32.t)
(h: HS.mem)
: Lemma
(requires
(valid (parse_deplen min max hp dlf ps) h input pos) /\
(valid_deplen_decomp min max hp dlf pp input pos h))
(ensures
(let pos_payload = get_valid_pos hp h input pos in
let len_payload = dlf (contents hp h input pos) in
let input' = { base = input.base; len = (pos_payload `U32.add` len_payload) } in
valid pp h input' pos_payload /\
(let pos_end' = get_valid_pos pp h input' pos_payload in
let pos_end = get_valid_pos (parse_deplen min max hp dlf ps) h input pos in
pos_end == pos_end'))) =
| valid_facts (parse_deplen min max hp dlf ps) h input pos;
valid_facts hp h input pos;
parse_deplen_unfold2 min max hp dlf ps (bytes_of_slice_from h input pos);
if valid_dec hp h input pos
then
let pos_payload = get_valid_pos hp h input pos in
let len = dlf (contents hp h input pos) in
if U32.v pos_payload + U32.v len <= U32.v input.len
then
let pos_end = pos_payload `U32.add` len in
valid_exact_equiv pp h input pos_payload pos_end;
valid_exact_valid_pos_equiv pp h input pos_payload pos_end;
let input' = { base = input.base; len = pos_end } in
valid_pos_get_valid_pos pp h input' pos_payload pos_end | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.return_pre | val return_pre (p: vprop) : vprop | val return_pre (p: vprop) : vprop | let return_pre (p:vprop) : vprop = p | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 36,
"end_line": 154,
"start_col": 0,
"start_line": 154
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Steel.Effect.Common.vprop -> Steel.Effect.Common.vprop | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.vprop"
] | [] | false | false | false | true | false | let return_pre (p: vprop) : vprop =
| p | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.can_be_split_forall | val can_be_split_forall : p: Steel.Effect.Common.post_t a -> q: Steel.Effect.Common.post_t a -> Prims.logical | let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 85,
"end_line": 173,
"start_col": 0,
"start_line": 173
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h) | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Steel.Effect.Common.post_t a -> q: Steel.Effect.Common.post_t a -> Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.post_t",
"Prims.l_Forall",
"Steel.Effect.Common.can_be_split",
"Prims.logical"
] | [] | false | false | false | true | true | let can_be_split_forall (#a: Type) (p q: post_t a) =
| forall x. can_be_split (p x) (q x) | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.can_be_split_dep | val can_be_split_dep : p: Prims.prop -> t1: Steel.Effect.Common.pre_t -> t2: Steel.Effect.Common.pre_t -> Prims.logical | let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2 | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 70,
"end_line": 176,
"start_col": 0,
"start_line": 176
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x) | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Prims.prop -> t1: Steel.Effect.Common.pre_t -> t2: Steel.Effect.Common.pre_t -> Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.prop",
"Steel.Effect.Common.pre_t",
"Prims.l_imp",
"Steel.Effect.Common.can_be_split",
"Prims.logical"
] | [] | false | false | false | true | true | let can_be_split_dep (p: prop) (t1 t2: pre_t) =
| p ==> can_be_split t1 t2 | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.hmem | val hmem : p: Steel.Effect.Common.vprop -> Type | let hmem (p:vprop) = hmem (hp_of p) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 35,
"end_line": 157,
"start_col": 0,
"start_line": 157
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Steel.Effect.Common.vprop -> Type | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.vprop",
"Steel.Memory.hmem",
"Steel.Effect.Common.hp_of"
] | [] | false | false | false | true | true | let hmem (p: vprop) =
| hmem (hp_of p) | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.can_be_split_forall_dep | val can_be_split_forall_dep : p: (_: a -> Prims.prop) -> t1: Steel.Effect.Common.post_t a -> t2: Steel.Effect.Common.post_t a
-> Prims.logical | let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 50,
"end_line": 180,
"start_col": 0,
"start_line": 179
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2 | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: (_: a -> Prims.prop) -> t1: Steel.Effect.Common.post_t a -> t2: Steel.Effect.Common.post_t a
-> Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.prop",
"Steel.Effect.Common.post_t",
"Prims.l_Forall",
"Prims.l_imp",
"Steel.Effect.Common.can_be_split",
"Prims.logical"
] | [] | false | false | false | true | true | let can_be_split_forall_dep (#a: Type) (p: (a -> prop)) (t1 t2: post_t a) =
| forall (x: a). p x ==> can_be_split (t1 x) (t2 x) | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.sel_depends_only_on_core | val sel_depends_only_on_core : sel: Steel.Effect.Common.selector' a hp -> Prims.logical | let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 50,
"end_line": 72,
"start_col": 0,
"start_line": 71
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | sel: Steel.Effect.Common.selector' a hp -> Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Steel.Memory.slprop",
"Steel.Effect.Common.selector'",
"Prims.l_Forall",
"Steel.Memory.hmem",
"Prims.eq2",
"Steel.Memory.core_mem",
"Prims.logical"
] | [] | false | false | false | false | true | let sel_depends_only_on_core (#a: Type) (#hp: slprop) (sel: selector' a hp) =
| forall (m0: hmem hp). sel m0 == sel (core_mem m0) | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.equiv_forall | val equiv_forall (#a: Type) (t1 t2: post_t a) : Type0 | val equiv_forall (#a: Type) (t1 t2: post_t a) : Type0 | let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1 | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 62,
"end_line": 258,
"start_col": 0,
"start_line": 257
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | t1: Steel.Effect.Common.post_t a -> t2: Steel.Effect.Common.post_t a -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.post_t",
"Prims.l_and",
"Steel.Effect.Common.can_be_split_forall"
] | [] | false | false | false | true | true | let equiv_forall (#a: Type) (t1 t2: post_t a) : Type0 =
| t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1 | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.prop_and | val prop_and (p1 p2: prop) : Tot prop | val prop_and (p1 p2: prop) : Tot prop | let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2 | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 48,
"end_line": 223,
"start_col": 0,
"start_line": 223
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q))) | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p1: Prims.prop -> p2: Prims.prop -> Prims.prop | Prims.Tot | [
"total"
] | [] | [
"Prims.prop",
"Prims.l_and"
] | [] | false | false | false | true | true | let prop_and (p1 p2: prop) : Tot prop =
| p1 /\ p2 | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.join_preserves_interp | val join_preserves_interp (hp: slprop) (m0: hmem hp) (m1: mem{disjoint m0 m1})
: Lemma (interp hp (join m0 m1)) [SMTPat (interp hp (join m0 m1))] | val join_preserves_interp (hp: slprop) (m0: hmem hp) (m1: mem{disjoint m0 m1})
: Lemma (interp hp (join m0 m1)) [SMTPat (interp hp (join m0 m1))] | let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 33,
"end_line": 53,
"start_col": 0,
"start_line": 46
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2 | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
hp: Steel.Memory.slprop ->
m0: Steel.Memory.hmem hp ->
m1: Steel.Memory.mem{Steel.Memory.disjoint m0 m1}
-> FStar.Pervasives.Lemma (ensures Steel.Memory.interp hp (Steel.Memory.join m0 m1))
[SMTPat (Steel.Memory.interp hp (Steel.Memory.join m0 m1))] | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Steel.Memory.slprop",
"Steel.Memory.hmem",
"Steel.Memory.mem",
"Steel.Memory.disjoint",
"Steel.Memory.affine_star",
"Steel.Memory.emp",
"Steel.Memory.join",
"Prims.unit",
"Steel.Memory.intro_star",
"Steel.Memory.intro_emp",
"Prims.l_True",
"Prims.squash",
"Steel.Memory.interp",
"Prims.Cons",
"FStar.Pervasives.pattern",
"FStar.Pervasives.smt_pat",
"Prims.prop",
"Prims.Nil"
] | [] | true | false | true | false | false | let join_preserves_interp (hp: slprop) (m0: hmem hp) (m1: mem{disjoint m0 m1})
: Lemma (interp hp (join m0 m1)) [SMTPat (interp hp (join m0 m1))] =
| let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1) | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.rmem' | val rmem' : pre: Steel.Effect.Common.vprop -> Type | let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0)) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 30,
"end_line": 279,
"start_col": 0,
"start_line": 276
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *) | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | pre: Steel.Effect.Common.vprop -> Type | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.vprop",
"FStar.FunctionalExtensionality.restricted_g_t",
"Steel.Effect.Common.can_be_split",
"Steel.Effect.Common.normal",
"Steel.Effect.Common.t_of"
] | [] | false | false | false | true | true | let rmem' (pre: vprop) =
| FExt.restricted_g_t (r0: vprop{can_be_split pre r0}) (fun r0 -> normal (t_of r0)) | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.rmem | val rmem : pre: Steel.Effect.Common.vprop -> Type | let rmem (pre:vprop) = h:rmem' pre{valid_rmem h} | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 48,
"end_line": 285,
"start_col": 0,
"start_line": 285
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | pre: Steel.Effect.Common.vprop -> Type | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.rmem'",
"Steel.Effect.Common.valid_rmem"
] | [] | false | false | false | true | true | let rmem (pre: vprop) =
| h: rmem' pre {valid_rmem h} | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.can_be_split_post | val can_be_split_post : t1: (_: a -> Steel.Effect.Common.post_t b) -> t2: Steel.Effect.Common.post_t b -> Prims.logical | let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2 | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 38,
"end_line": 264,
"start_col": 0,
"start_line": 263
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | t1: (_: a -> Steel.Effect.Common.post_t b) -> t2: Steel.Effect.Common.post_t b -> Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.post_t",
"Prims.l_Forall",
"Steel.Effect.Common.equiv_forall",
"Prims.logical"
] | [] | false | false | false | true | true | let can_be_split_post (#a #b: Type) (t1: (a -> post_t b)) (t2: post_t b) =
| forall (x: a). equiv_forall (t1 x) t2 | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.maybe_emp_dep | val maybe_emp_dep : framed: Prims.bool -> frame: Steel.Effect.Common.post_t a -> Prims.logical | let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 53,
"end_line": 333,
"start_col": 0,
"start_line": 332
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | framed: Prims.bool -> frame: Steel.Effect.Common.post_t a -> Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.bool",
"Steel.Effect.Common.post_t",
"Prims.l_Forall",
"Prims.eq2",
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.emp",
"Prims.l_True",
"Prims.logical"
] | [] | false | false | false | true | true | let maybe_emp_dep (#a: Type) (framed: bool) (frame: post_t a) =
| if framed then (forall x. frame x == emp) else True | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.sel_depends_only_on | val sel_depends_only_on : sel: Steel.Effect.Common.selector' a hp -> Prims.logical | let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1))) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 32,
"end_line": 67,
"start_col": 0,
"start_line": 64
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | sel: Steel.Effect.Common.selector' a hp -> Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Steel.Memory.slprop",
"Steel.Effect.Common.selector'",
"Prims.l_Forall",
"Steel.Memory.hmem",
"Steel.Memory.mem",
"Steel.Memory.disjoint",
"Prims.eq2",
"Steel.Memory.join",
"Prims.unit",
"Steel.Memory.interp_depends_only_on",
"Prims.logical"
] | [] | false | false | false | false | true | let sel_depends_only_on (#a: Type) (#hp: slprop) (sel: selector' a hp) =
| forall (m0: hmem hp) (m1: mem{disjoint m0 m1}).
(interp_depends_only_on hp;
(sel m0 == sel (join m0 m1))) | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.maybe_emp | val maybe_emp : framed: Prims.bool -> frame: Steel.Effect.Common.pre_t -> Prims.logical | let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 81,
"end_line": 330,
"start_col": 0,
"start_line": 330
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | framed: Prims.bool -> frame: Steel.Effect.Common.pre_t -> Prims.logical | Prims.Tot | [
"total"
] | [] | [
"Prims.bool",
"Steel.Effect.Common.pre_t",
"Prims.eq2",
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.emp",
"Prims.l_True",
"Prims.logical"
] | [] | false | false | false | true | true | let maybe_emp (framed: bool) (frame: pre_t) =
| if framed then frame == emp else True | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.pure | val pure : p: Prims.prop -> Steel.Effect.Common.vprop | let pure (p:prop) = to_vprop (pure p) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 44,
"end_line": 326,
"start_col": 7,
"start_line": 326
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Prims.prop -> Steel.Effect.Common.vprop | Prims.Tot | [
"total"
] | [] | [
"Prims.prop",
"Steel.Effect.Common.to_vprop",
"Steel.Memory.pure",
"Steel.Effect.Common.vprop"
] | [] | false | false | false | true | false | let pure (p: prop) =
| to_vprop (pure p) | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.focus_rmem' | val focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0) | val focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0) | let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 33,
"end_line": 348,
"start_col": 0,
"start_line": 345
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r' | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
h: Steel.Effect.Common.rmem r ->
r0: Steel.Effect.Common.vprop{Steel.Effect.Common.can_be_split r r0}
-> Steel.Effect.Common.rmem' r0 | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.rmem",
"Steel.Effect.Common.can_be_split",
"FStar.FunctionalExtensionality.on_dom_g",
"Steel.Effect.Common.normal",
"Steel.Effect.Common.t_of",
"Steel.Effect.Common.unrestricted_focus_rmem",
"Steel.Effect.Common.rmem'"
] | [] | false | false | false | false | false | let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0) =
| FExt.on_dom_g (r': vprop{can_be_split r0 r'}) (unrestricted_focus_rmem h r0) | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.frame_equalities | val frame_equalities (frame: vprop) (h0 h1: rmem frame) : prop | val frame_equalities (frame: vprop) (h0 h1: rmem frame) : prop | let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 69,
"end_line": 463,
"start_col": 0,
"start_line": 460
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__] | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
frame: Steel.Effect.Common.vprop ->
h0: Steel.Effect.Common.rmem frame ->
h1: Steel.Effect.Common.rmem frame
-> Prims.prop | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.rmem",
"FStar.Tactics.Effect.rewrite_with_tactic",
"Steel.Effect.Common.frame_vc_norm",
"Steel.Effect.Common.frame_equalities'",
"Prims.prop"
] | [] | false | false | false | false | true | let frame_equalities (frame: vprop) (h0 h1: rmem frame) : prop =
| rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1) | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.frame_vc_norm | val frame_vc_norm: Prims.unit -> Tac unit | val frame_vc_norm: Prims.unit -> Tac unit | let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ()) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 11,
"end_line": 456,
"start_col": 0,
"start_line": 417
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Prims.unit -> FStar.Tactics.Effect.Tac Prims.unit | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.unit",
"FStar.Stubs.Tactics.V2.Builtins.with_compat_pre_core",
"FStar.Tactics.V2.Derived.trefl",
"FStar.Stubs.Tactics.V2.Builtins.norm",
"Steel.Effect.Common.normal_steps",
"FStar.Tactics.V2.Derived.or_else",
"FStar.Tactics.V2.Derived.dismiss",
"FStar.Tactics.V2.Derived.apply_lemma",
"FStar.Pervasives.ignore",
"Prims.list",
"FStar.Tactics.V2.Derived.repeat",
"FStar.Tactics.V2.Logic.split",
"Prims.Cons",
"FStar.Pervasives.norm_step",
"FStar.Pervasives.delta_attr",
"Prims.string",
"Prims.Nil",
"FStar.Pervasives.delta_only",
"FStar.Pervasives.delta_qualifier",
"FStar.Pervasives.iota",
"FStar.Pervasives.zeta",
"FStar.Pervasives.primops",
"FStar.Pervasives.simplify"
] | [] | false | true | false | false | false | let frame_vc_norm () : Tac unit =
| with_compat_pre_core 0
(fun _ ->
norm [
delta_attr [`%__steel_reduce__];
delta_only [
`%Mkvprop'?.t;
`%Mkvprop'?.hp;
`%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit
];
delta_qualifier ["unfold"];
iota;
zeta;
primops;
simplify
];
ignore (repeat (fun _ ->
apply_lemma (`elim_conjunction);
dismiss ();
dismiss ();
split ();
or_else (fun _ ->
apply_lemma (`lemma_frame_emp);
dismiss ())
(fun _ -> ());
or_else (fun _ ->
apply_lemma (`lemma_frame_equalities);
dismiss ())
(fun _ -> ());
norm normal_steps;
trefl ()));
or_else (fun _ ->
apply_lemma (`lemma_frame_emp);
dismiss ())
(fun _ -> ());
or_else (fun _ ->
apply_lemma (`lemma_frame_equalities);
dismiss ())
(fun _ -> ());
norm normal_steps;
trefl ()) | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.req | val req:CE.equiv vprop | val req:CE.equiv vprop | let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 16,
"end_line": 511,
"start_col": 32,
"start_line": 507
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | FStar.Algebra.CommMonoid.Equiv.equiv Steel.Effect.Common.vprop | Prims.Tot | [
"total"
] | [] | [
"FStar.Algebra.CommMonoid.Equiv.EQ",
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.equiv",
"Steel.Effect.Common.equiv_refl",
"Steel.Effect.Common.equiv_sym",
"Steel.Effect.Common.equiv_trans"
] | [] | false | false | false | true | false | let req:CE.equiv vprop =
| CE.EQ equiv equiv_refl equiv_sym equiv_trans | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.hp_of | val hp_of : p: Steel.Effect.Common.vprop -> Steel.Memory.slprop | let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2 | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 47,
"end_line": 130,
"start_col": 0,
"start_line": 128
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Steel.Effect.Common.vprop -> Steel.Memory.slprop | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.vprop'",
"Steel.Effect.Common.__proj__Mkvprop'__item__hp",
"Steel.Memory.star",
"Steel.Effect.Common.hp_of",
"Steel.Memory.slprop"
] | [
"recursion"
] | false | false | false | true | false | let rec hp_of (p: vprop) =
| match p with
| VUnit p -> p.hp
| VStar p1 p2 -> (hp_of p1) `Mem.star` (hp_of p2) | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.can_be_split_trans_rev | val can_be_split_trans_rev (p q r: vprop)
: Lemma (requires q `can_be_split` r /\ p `can_be_split` q) (ensures p `can_be_split` r) | val can_be_split_trans_rev (p q r: vprop)
: Lemma (requires q `can_be_split` r /\ p `can_be_split` q) (ensures p `can_be_split` r) | let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 26,
"end_line": 194,
"start_col": 0,
"start_line": 190
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r) | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Steel.Effect.Common.vprop -> q: Steel.Effect.Common.vprop -> r: Steel.Effect.Common.vprop
-> FStar.Pervasives.Lemma
(requires Steel.Effect.Common.can_be_split q r /\ Steel.Effect.Common.can_be_split p q)
(ensures Steel.Effect.Common.can_be_split p r) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.can_be_split_trans",
"Prims.unit",
"Prims.l_and",
"Steel.Effect.Common.can_be_split",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | true | false | true | false | false | let can_be_split_trans_rev (p q r: vprop)
: Lemma (requires q `can_be_split` r /\ p `can_be_split` q) (ensures p `can_be_split` r) =
| can_be_split_trans p q r | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.vrefine | val vrefine : v: Steel.Effect.Common.vprop ->
p: (_: Steel.Effect.Common.normal (Steel.Effect.Common.t_of v) -> Prims.prop)
-> Steel.Effect.Common.vprop | let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 80,
"end_line": 576,
"start_col": 0,
"start_line": 576
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
} | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
v: Steel.Effect.Common.vprop ->
p: (_: Steel.Effect.Common.normal (Steel.Effect.Common.t_of v) -> Prims.prop)
-> Steel.Effect.Common.vprop | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.normal",
"Steel.Effect.Common.t_of",
"Prims.prop",
"Steel.Effect.Common.VUnit",
"Steel.Effect.Common.vrefine'"
] | [] | false | false | false | false | false | let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) =
| VUnit (vrefine' v p) | false |
|
Hacl.Spec.K256.MathLemmas.fst | Hacl.Spec.K256.MathLemmas.lemma_simplify_carry_round | val lemma_simplify_carry_round (t0 t1 t2 t3 t4:nat) : Lemma
(let a = t1 + t0 / pow2 52 in
let b = t2 + a / pow2 52 in
let c = t3 + b / pow2 52 in
let d = t4 + c / pow2 52 in
t0 % pow2 52 + (a % pow2 52) * pow2 52 + (b % pow2 52) * pow2 104 +
(c % pow2 52) * pow2 156 + d * pow2 208 ==
t0 + t1 * pow2 52 + t2 * pow2 104 + t3 * pow2 156 + t4 * pow2 208) | val lemma_simplify_carry_round (t0 t1 t2 t3 t4:nat) : Lemma
(let a = t1 + t0 / pow2 52 in
let b = t2 + a / pow2 52 in
let c = t3 + b / pow2 52 in
let d = t4 + c / pow2 52 in
t0 % pow2 52 + (a % pow2 52) * pow2 52 + (b % pow2 52) * pow2 104 +
(c % pow2 52) * pow2 156 + d * pow2 208 ==
t0 + t1 * pow2 52 + t2 * pow2 104 + t3 * pow2 156 + t4 * pow2 208) | let lemma_simplify_carry_round t0 t1 t2 t3 t4 =
let a = t1 + t0 / pow2 52 in
let b = t2 + a / pow2 52 in
let c = t3 + b / pow2 52 in
let d = t4 + c / pow2 52 in
calc (==) {
t0 % pow2 52 + (a % pow2 52) * pow2 52 + (b % pow2 52) * pow2 104 +
(c % pow2 52) * pow2 156 + d * pow2 208;
(==) { lemma_a_mod_52_mul_b a 52 }
t0 % pow2 52 + (t1 + t0 / pow2 52) * pow2 52 - a / pow2 52 * pow2 104 + (b % pow2 52) * pow2 104 +
(c % pow2 52) * pow2 156 + d * pow2 208;
(==) { lemma_distr_eucl t0 t1 }
t0 + t1 * pow2 52 - a / pow2 52 * pow2 104 + (b % pow2 52) * pow2 104 +
(c % pow2 52) * pow2 156 + d * pow2 208;
(==) { lemma_a_mod_52_mul_b b 104 }
t0 + t1 * pow2 52 - a / pow2 52 * pow2 104 + (t2 + a / pow2 52) * pow2 104 - b / pow2 52 * pow2 156 +
(c % pow2 52) * pow2 156 + d * pow2 208;
(==) { Math.Lemmas.distributivity_add_left t2 (a / pow2 52) (pow2 104) }
t0 + t1 * pow2 52 + t2 * pow2 104 - b / pow2 52 * pow2 156 +
(c % pow2 52) * pow2 156 + d * pow2 208;
(==) { lemma_a_mod_52_mul_b c 156 }
t0 + t1 * pow2 52 + t2 * pow2 104 - b / pow2 52 * pow2 156 +
(t3 + b / pow2 52) * pow2 156 - c / pow2 52 * pow2 208 + d * pow2 208;
(==) { Math.Lemmas.distributivity_add_left t3 (b / pow2 52) (pow2 156) }
t0 + t1 * pow2 52 + t2 * pow2 104 + t3 * pow2 156 - c / pow2 52 * pow2 208 + (t4 + c / pow2 52) * pow2 208;
(==) { Math.Lemmas.distributivity_add_left t4 (c / pow2 52) (pow2 208) }
t0 + t1 * pow2 52 + t2 * pow2 104 + t3 * pow2 156 + t4 * pow2 208;
} | {
"file_name": "code/k256/Hacl.Spec.K256.MathLemmas.fst",
"git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872",
"git_url": "https://github.com/project-everest/hacl-star.git",
"project_name": "hacl-star"
} | {
"end_col": 3,
"end_line": 347,
"start_col": 0,
"start_line": 319
} | module Hacl.Spec.K256.MathLemmas
open FStar.Mul
#set-options "--z3rlimit 50 --fuel 0 --ifuel 0"
val lemma_swap_mul3 (a b c:int) : Lemma (a * b * c == a * c * b)
let lemma_swap_mul3 a b c =
calc (==) {
a * b * c;
(==) { Math.Lemmas.paren_mul_right a b c }
a * (b * c);
(==) { Math.Lemmas.swap_mul b c }
a * (c * b);
(==) { Math.Lemmas.paren_mul_right a c b }
a * c * b;
}
val lemma_mod_mul_distr (a b:int) (n:pos) : Lemma (a * b % n = (a % n) * (b % n) % n)
let lemma_mod_mul_distr a b n =
Math.Lemmas.lemma_mod_mul_distr_l a b n;
Math.Lemmas.lemma_mod_mul_distr_r (a % n) b n
val lemma_mod_sub_distr (a b:int) (n:pos) : Lemma ((a - b) % n = (a % n - b % n) % n)
let lemma_mod_sub_distr a b n =
Math.Lemmas.lemma_mod_plus_distr_l a (- b) n;
Math.Lemmas.lemma_mod_sub_distr (a % n) b n
val lemma_ab_le_cd (a b c d:nat) : Lemma
(requires a <= c /\ b <= d)
(ensures a * b <= c * d)
let lemma_ab_le_cd a b c d =
Math.Lemmas.lemma_mult_le_left a b d;
Math.Lemmas.lemma_mult_le_right d a c
val lemma_ab_lt_cd (a b c d:pos) : Lemma
(requires a < c /\ b < d)
(ensures a * b < c * d)
let lemma_ab_lt_cd a b c d =
Math.Lemmas.lemma_mult_lt_left a b d;
Math.Lemmas.lemma_mult_lt_right d a c
val lemma_bound_mul64_wide (ma mb:nat) (mma mmb:nat) (a b:nat) : Lemma
(requires a <= ma * mma /\ b <= mb * mmb)
(ensures a * b <= ma * mb * (mma * mmb))
let lemma_bound_mul64_wide ma mb mma mmb a b =
calc (<=) {
a * b;
(<=) { lemma_ab_le_cd a b (ma * mma) (mb * mmb) }
(ma * mma) * (mb * mmb);
(==) { Math.Lemmas.paren_mul_right ma mma (mb * mmb) }
ma * (mma * (mb * mmb));
(==) {
Math.Lemmas.paren_mul_right mma mb mmb;
Math.Lemmas.swap_mul mma mb;
Math.Lemmas.paren_mul_right mb mma mmb }
ma * (mb * (mma * mmb));
(==) { Math.Lemmas.paren_mul_right ma mb (mma * mmb) }
ma * mb * (mma * mmb);
}
val lemma_distr_pow (a b:int) (c d:nat) : Lemma ((a + b * pow2 c) * pow2 d = a * pow2 d + b * pow2 (c + d))
let lemma_distr_pow a b c d =
calc (==) {
(a + b * pow2 c) * pow2 d;
(==) { Math.Lemmas.distributivity_add_left a (b * pow2 c) (pow2 d) }
a * pow2 d + b * pow2 c * pow2 d;
(==) { Math.Lemmas.paren_mul_right b (pow2 c) (pow2 d); Math.Lemmas.pow2_plus c d }
a * pow2 d + b * pow2 (c + d);
}
val lemma_distr_pow_pow (a:int) (b:nat) (c:int) (d e:nat) :
Lemma ((a * pow2 b + c * pow2 d) * pow2 e = a * pow2 (b + e) + c * pow2 (d + e))
let lemma_distr_pow_pow a b c d e =
calc (==) {
(a * pow2 b + c * pow2 d) * pow2 e;
(==) { lemma_distr_pow (a * pow2 b) c d e }
a * pow2 b * pow2 e + c * pow2 (d + e);
(==) { Math.Lemmas.paren_mul_right a (pow2 b) (pow2 e); Math.Lemmas.pow2_plus b e }
a * pow2 (b + e) + c * pow2 (d + e);
}
val lemma_distr_eucl_mul (r a:int) (b:pos) : Lemma (r * (a % b) + r * (a / b) * b = r * a)
let lemma_distr_eucl_mul r a b =
calc (==) {
r * (a % b) + r * (a / b) * b;
(==) { Math.Lemmas.paren_mul_right r (a / b) b }
r * (a % b) + r * ((a / b) * b);
(==) { Math.Lemmas.distributivity_add_right r (a % b) (a / b * b) }
r * (a % b + a / b * b);
(==) { Math.Lemmas.euclidean_division_definition a b }
r * a;
}
val lemma_distr_eucl_mul_add (r a c:int) (b:pos) : Lemma (r * (a % b) + r * (a / b + c) * b = r * a + r * c * b)
let lemma_distr_eucl_mul_add r a c b =
calc (==) {
r * (a % b) + r * (a / b + c) * b;
(==) { Math.Lemmas.paren_mul_right r (a / b + c) b }
r * (a % b) + r * ((a / b + c) * b);
(==) { Math.Lemmas.distributivity_add_left (a / b) c b }
r * (a % b) + r * ((a / b * b) + c * b);
(==) { Math.Lemmas.distributivity_add_right r (a / b * b) (c * b) }
r * (a % b) + r * (a / b * b) + r * (c * b);
(==) { Math.Lemmas.paren_mul_right r (a / b) b; Math.Lemmas.paren_mul_right r c b }
r * (a % b) + r * (a / b) * b + r * c * b;
(==) { lemma_distr_eucl_mul r a b }
r * a + r * c * b;
}
val lemma_distr_eucl (a b:int) : Lemma ((a / pow2 52 + b) * pow2 52 + a % pow2 52 = a + b * pow2 52)
let lemma_distr_eucl a b = lemma_distr_eucl_mul_add 1 a b (pow2 52)
val lemma_a_plus_b_pow2_mod2 (a b:int) (c:pos) : Lemma ((a + b * pow2 c) % 2 = a % 2)
let lemma_a_plus_b_pow2_mod2 a b c =
assert_norm (pow2 1 = 2);
Math.Lemmas.lemma_mod_plus_distr_r a (b * pow2 c) 2;
Math.Lemmas.pow2_multiplication_modulo_lemma_1 b 1 c
val lemma_as_nat64_horner (r0 r1 r2 r3:int) :
Lemma (r0 + r1 * pow2 64 + r2 * pow2 128 + r3 * pow2 192 ==
((r3 * pow2 64 + r2) * pow2 64 + r1) * pow2 64 + r0)
let lemma_as_nat64_horner r0 r1 r2 r3 =
calc (==) {
r0 + pow2 64 * (r1 + pow2 64 * (r2 + pow2 64 * r3));
(==) { Math.Lemmas.swap_mul (pow2 64) (r1 + pow2 64 * (r2 + pow2 64 * r3)) }
r0 + (r1 + pow2 64 * (r2 + pow2 64 * r3)) * pow2 64;
(==) { Math.Lemmas.swap_mul (pow2 64) (r2 + pow2 64 * r3) }
r0 + (r1 + (r2 + pow2 64 * r3) * pow2 64) * pow2 64;
(==) { lemma_distr_pow r1 (r2 + pow2 64 * r3) 64 64 }
r0 + r1 * pow2 64 + (r2 + pow2 64 * r3) * pow2 128;
(==) { Math.Lemmas.swap_mul (pow2 64) r3 }
r0 + r1 * pow2 64 + (r2 + r3 * pow2 64) * pow2 128;
(==) { lemma_distr_pow r2 r3 64 128 }
r0 + r1 * pow2 64 + r2 * pow2 128 + r3 * pow2 192;
}
val lemma_as_nat_horner (r0 r1 r2 r3 r4:int) :
Lemma (r0 + r1 * pow2 52 + r2 * pow2 104 + r3 * pow2 156 + r4 * pow2 208 ==
(((r4 * pow2 52 + r3) * pow2 52 + r2) * pow2 52 + r1) * pow2 52 + r0)
let lemma_as_nat_horner r0 r1 r2 r3 r4 =
calc (==) {
(((r4 * pow2 52 + r3) * pow2 52 + r2) * pow2 52 + r1) * pow2 52 + r0;
(==) { lemma_distr_pow r1 ((r4 * pow2 52 + r3) * pow2 52 + r2) 52 52 }
((r4 * pow2 52 + r3) * pow2 52 + r2) * pow2 104 + r1 * pow2 52 + r0;
(==) { lemma_distr_pow r2 (r4 * pow2 52 + r3) 52 104 }
(r4 * pow2 52 + r3) * pow2 156 + r2 * pow2 104 + r1 * pow2 52 + r0;
(==) { lemma_distr_pow r3 r4 52 156 }
r4 * pow2 208 + r3 * pow2 156 + r2 * pow2 104 + r1 * pow2 52 + r0;
}
val lemma_distr5 (a0 a1 a2 a3 a4 b:int) : Lemma
((a0 + a1 + a2 + a3 + a4) * b = a0 * b + a1 * b + a2 * b + a3 * b + a4 * b)
let lemma_distr5 a0 a1 a2 a3 a4 b =
calc (==) {
(a0 + a1 + a2 + a3 + a4) * b;
(==) { Math.Lemmas.distributivity_add_left a0 (a1 + a2 + a3 + a4) b }
a0 * b + (a1 + a2 + a3 + a4) * b;
(==) { Math.Lemmas.distributivity_add_left a1 (a2 + a3 + a4) b }
a0 * b + a1 * b + (a2 + a3 + a4) * b;
(==) { Math.Lemmas.distributivity_add_left a2 (a3 + a4) b }
a0 * b + a1 * b + a2 * b + (a3 + a4) * b;
(==) { Math.Lemmas.distributivity_add_left a3 a4 b }
a0 * b + a1 * b + a2 * b + a3 * b + a4 * b;
}
val lemma_distr5_pow52 (a b0 b1 b2 b3 b4:int) : Lemma
(a * (b0 + b1 * pow2 52 + b2 * pow2 104 + b3 * pow2 156 + b4 * pow2 208) =
a * b0 + a * b1 * pow2 52 + a * b2 * pow2 104 + a * b3 * pow2 156 + a * b4 * pow2 208)
let lemma_distr5_pow52 a b0 b1 b2 b3 b4 =
calc (==) {
a * (b0 + b1 * pow2 52 + b2 * pow2 104 + b3 * pow2 156 + b4 * pow2 208);
(==) { lemma_distr5 b0 (b1 * pow2 52) (b2 * pow2 104) (b3 * pow2 156) (b4 * pow2 208) a }
b0 * a + b1 * pow2 52 * a + b2 * pow2 104 * a + b3 * pow2 156 * a + b4 * pow2 208 * a;
(==) { lemma_swap_mul3 b1 (pow2 52) a; lemma_swap_mul3 b2 (pow2 104) a }
b0 * a + b1 * a * pow2 52 + b2 * a * pow2 104 + b3 * pow2 156 * a + b4 * pow2 208 * a;
(==) { lemma_swap_mul3 b3 (pow2 156) a; lemma_swap_mul3 b4 (pow2 208) a }
b0 * a + b1 * a * pow2 52 + b2 * a * pow2 104 + b3 * a * pow2 156 + b4 * a * pow2 208;
}
val lemma_distr5_pow52_mul_pow (a b0 b1 b2 b3 b4: int) (p:nat) : Lemma
(a * pow2 p * (b0 + b1 * pow2 52 + b2 * pow2 104 + b3 * pow2 156 + b4 * pow2 208) =
a * b0 * pow2 p + a * b1 * pow2 (52 + p) + a * b2 * pow2 (104 + p) +
a * b3 * pow2 (156 + p) + a * b4 * pow2 (208 + p))
let lemma_distr5_pow52_mul_pow a b0 b1 b2 b3 b4 p =
let b_sum = b0 + b1 * pow2 52 + b2 * pow2 104 + b3 * pow2 156 + b4 * pow2 208 in
calc (==) {
a * pow2 p * b_sum;
(==) { lemma_swap_mul3 a (pow2 p) b_sum }
a * b_sum * pow2 p;
(==) { lemma_distr5_pow52 a b0 b1 b2 b3 b4 }
(a * b0 + a * b1 * pow2 52 + a * b2 * pow2 104 + a * b3 * pow2 156 + a * b4 * pow2 208) * pow2 p;
(==) { lemma_distr_pow (a * b0 + a * b1 * pow2 52 + a * b2 * pow2 104 + a * b3 * pow2 156) (a * b4) 208 p }
(a * b0 + a * b1 * pow2 52 + a * b2 * pow2 104 + a * b3 * pow2 156) * pow2 p + a * b4 * pow2 (208 + p);
(==) { lemma_distr_pow (a * b0 + a * b1 * pow2 52 + a * b2 * pow2 104) (a * b3) 156 p }
(a * b0 + a * b1 * pow2 52 + a * b2 * pow2 104) * pow2 p + a * b3 * pow2 (156 + p) + a * b4 * pow2 (208 + p);
(==) { lemma_distr_pow (a * b0 + a * b1 * pow2 52) (a * b2) 104 p }
(a * b0 + a * b1 * pow2 52) * pow2 p + a * b2 * pow2 (104 + p) + a * b3 * pow2 (156 + p) + a * b4 * pow2 (208 + p);
(==) { lemma_distr_pow (a * b0) (a * b1) 52 p }
a * b0 * pow2 p + a * b1 * pow2 (52 + p) + a * b2 * pow2 (104 + p) + a * b3 * pow2 (156 + p) + a * b4 * pow2 (208 + p);
}
val lemma_distr5_pow52_sub (a0 a1 a2 a3 a4 b0 b1 b2 b3 b4 c:int) : Lemma
((b0 * c - a0) + (b1 * c - a1) * pow2 52 + (b2 * c - a2) * pow2 104 +
(b3 * c - a3) * pow2 156 + (b4 * c - a4) * pow2 208 ==
(b0 + b1 * pow2 52 + b2 * pow2 104 + b3 * pow2 156 + b4 * pow2 208) * c -
(a0 + a1 * pow2 52 + a2 * pow2 104 + a3 * pow2 156 + a4 * pow2 208))
let lemma_distr5_pow52_sub a0 a1 a2 a3 a4 b0 b1 b2 b3 b4 c =
calc (==) {
(b0 * c - a0) + (b1 * c - a1) * pow2 52 + (b2 * c - a2) * pow2 104 +
(b3 * c - a3) * pow2 156 + (b4 * c - a4) * pow2 208;
(==) { Math.Lemmas.distributivity_sub_left (b1 * c) a1 (pow2 52) }
c * b0 - a0 + c * b1 * pow2 52 - a1 * pow2 52 + (b2 * c - a2) * pow2 104 +
(b3 * c - a3) * pow2 156 + (b4 * c - a4) * pow2 208;
(==) { Math.Lemmas.distributivity_sub_left (b2 * c) a2 (pow2 104) }
c * b0 - a0 + c * b1 * pow2 52 - a1 * pow2 52 + c * b2 * pow2 104 - a2 * pow2 104 +
(b3 * c - a3) * pow2 156 + (b4 * c - a4) * pow2 208;
(==) { Math.Lemmas.distributivity_sub_left (b3 * c) a3 (pow2 156) }
c * b0 - a0 + c * b1 * pow2 52 - a1 * pow2 52 + c * b2 * pow2 104 - a2 * pow2 104 +
c * b3 * pow2 156 - a3 * pow2 156 + (b4 * c - a4) * pow2 208;
(==) { Math.Lemmas.distributivity_sub_left (b4 * c) a4 (pow2 208) }
c * b0 - a0 + c * b1 * pow2 52 - a1 * pow2 52 + c * b2 * pow2 104 - a2 * pow2 104 +
c * b3 * pow2 156 - a3 * pow2 156 + c * b4 * pow2 208 - a4 * pow2 208;
(==) { lemma_distr5_pow52 c b0 b1 b2 b3 b4 }
(b0 + b1 * pow2 52 + b2 * pow2 104 + b3 * pow2 156 + b4 * pow2 208) * c -
(a0 + a1 * pow2 52 + a2 * pow2 104 + a3 * pow2 156 + a4 * pow2 208);
}
val lemma_a_div_b_plus_c_mod_d_mul_e (a b c d e:nat) : Lemma
(requires a / pow2 b < pow2 e)
(ensures a / pow2 b + c % pow2 d * pow2 e < pow2 (d + e))
let lemma_a_div_b_plus_c_mod_d_mul_e a b c d e =
let t_r = c % pow2 d * pow2 e in
Math.Lemmas.lemma_mult_le_right (pow2 e) (c % pow2 d) (pow2 d - 1);
assert (t_r <= (pow2 d - 1) * pow2 e);
assert (t_r <= pow2 d * pow2 e - pow2 e);
Math.Lemmas.pow2_plus d e;
assert (t_r <= pow2 (d + e) - pow2 e);
assert (a / pow2 b + c % pow2 d * pow2 e < pow2 (d + e))
val lemma_a_mod_b_mul_c_mod_d (a b c d:nat) : Lemma
(requires c <= d /\ b <= d - c)
(ensures (a % pow2 b) * pow2 c % pow2 d = (a % pow2 b) * pow2 c)
let lemma_a_mod_b_mul_c_mod_d a b c d =
Math.Lemmas.pow2_multiplication_modulo_lemma_2 (a % pow2 b) d c;
Math.Lemmas.pow2_modulo_modulo_lemma_2 a (d - c) b
val lemma_a_mul_c_plus_d_mod_e_mul_f_g (a b c d f g:nat) : Lemma
(requires c == g - b)
(ensures
a % pow2 b * pow2 c + (a / pow2 b + d * pow2 f) * pow2 g ==
a * pow2 c + d * pow2 (f + g))
let lemma_a_mul_c_plus_d_mod_e_mul_f_g a b c d f g =
calc (==) {
a % pow2 b * pow2 c + (a / pow2 b + d * pow2 f) * pow2 g;
(==) { lemma_distr_pow (a / pow2 b) d f g }
a % pow2 b * pow2 c + (a / pow2 b) * pow2 (c + b) + d * pow2 (f + g);
(==) { lemma_distr_pow (a % pow2 b) (a / pow2 b) b c }
(a % pow2 b + (a / pow2 b) * pow2 b) * pow2 c + d * pow2 (f + g);
(==) { Math.Lemmas.euclidean_division_definition a (pow2 b) }
a * pow2 c + d * pow2 (f + g);
}
val lemma_a_mod_52_mul_b (a b:nat) :
Lemma ((a % pow2 52) * pow2 b = a * pow2 b - a / pow2 52 * pow2 (b + 52))
let lemma_a_mod_52_mul_b a b =
calc (==) {
(a % pow2 52) * pow2 b;
(==) { Math.Lemmas.euclidean_division_definition a (pow2 52) }
(a - a / pow2 52 * pow2 52) * pow2 b;
(==) { Math.Lemmas.distributivity_sub_left a (a / pow2 52 * pow2 52) (pow2 b) }
a * pow2 b - a / pow2 52 * pow2 52 * pow2 b;
(==) { Math.Lemmas.paren_mul_right (a / pow2 52) (pow2 52) (pow2 b); Math.Lemmas.pow2_plus 52 b }
a * pow2 b - a / pow2 52 * pow2 (52 + b);
}
val lemma_simplify_carry_round (t0 t1 t2 t3 t4:nat) : Lemma
(let a = t1 + t0 / pow2 52 in
let b = t2 + a / pow2 52 in
let c = t3 + b / pow2 52 in
let d = t4 + c / pow2 52 in
t0 % pow2 52 + (a % pow2 52) * pow2 52 + (b % pow2 52) * pow2 104 +
(c % pow2 52) * pow2 156 + d * pow2 208 ==
t0 + t1 * pow2 52 + t2 * pow2 104 + t3 * pow2 156 + t4 * pow2 208) | {
"checked_file": "/",
"dependencies": [
"prims.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.Mul.fst.checked",
"FStar.Math.Lemmas.fst.checked",
"FStar.Calc.fsti.checked"
],
"interface_file": false,
"source_file": "Hacl.Spec.K256.MathLemmas.fst"
} | [
{
"abbrev": false,
"full_module": "FStar.Mul",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Spec.K256",
"short_module": null
},
{
"abbrev": false,
"full_module": "Hacl.Spec.K256",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": false,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 50,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | t0: Prims.nat -> t1: Prims.nat -> t2: Prims.nat -> t3: Prims.nat -> t4: Prims.nat
-> FStar.Pervasives.Lemma
(ensures
(let a = t1 + t0 / Prims.pow2 52 in
let b = t2 + a / Prims.pow2 52 in
let c = t3 + b / Prims.pow2 52 in
let d = t4 + c / Prims.pow2 52 in
t0 % Prims.pow2 52 + (a % Prims.pow2 52) * Prims.pow2 52 +
(b % Prims.pow2 52) * Prims.pow2 104 +
(c % Prims.pow2 52) * Prims.pow2 156 +
d * Prims.pow2 208 ==
t0 + t1 * Prims.pow2 52 + t2 * Prims.pow2 104 + t3 * Prims.pow2 156 + t4 * Prims.pow2 208)) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.nat",
"FStar.Calc.calc_finish",
"Prims.int",
"Prims.eq2",
"Prims.op_Addition",
"Prims.op_Modulus",
"Prims.pow2",
"FStar.Mul.op_Star",
"Prims.Cons",
"FStar.Preorder.relation",
"Prims.Nil",
"Prims.unit",
"FStar.Calc.calc_step",
"Prims.op_Subtraction",
"Prims.op_Division",
"FStar.Calc.calc_init",
"FStar.Calc.calc_pack",
"Hacl.Spec.K256.MathLemmas.lemma_a_mod_52_mul_b",
"Prims.squash",
"Hacl.Spec.K256.MathLemmas.lemma_distr_eucl",
"FStar.Math.Lemmas.distributivity_add_left"
] | [] | false | false | true | false | false | let lemma_simplify_carry_round t0 t1 t2 t3 t4 =
| let a = t1 + t0 / pow2 52 in
let b = t2 + a / pow2 52 in
let c = t3 + b / pow2 52 in
let d = t4 + c / pow2 52 in
calc ( == ) {
t0 % pow2 52 + (a % pow2 52) * pow2 52 + (b % pow2 52) * pow2 104 + (c % pow2 52) * pow2 156 +
d * pow2 208;
( == ) { lemma_a_mod_52_mul_b a 52 }
t0 % pow2 52 + (t1 + t0 / pow2 52) * pow2 52 - (a / pow2 52) * pow2 104 + (b % pow2 52) * pow2 104 +
(c % pow2 52) * pow2 156 +
d * pow2 208;
( == ) { lemma_distr_eucl t0 t1 }
t0 + t1 * pow2 52 - (a / pow2 52) * pow2 104 + (b % pow2 52) * pow2 104 + (c % pow2 52) * pow2 156 +
d * pow2 208;
( == ) { lemma_a_mod_52_mul_b b 104 }
t0 + t1 * pow2 52 - (a / pow2 52) * pow2 104 + (t2 + a / pow2 52) * pow2 104 -
(b / pow2 52) * pow2 156 +
(c % pow2 52) * pow2 156 +
d * pow2 208;
( == ) { Math.Lemmas.distributivity_add_left t2 (a / pow2 52) (pow2 104) }
t0 + t1 * pow2 52 + t2 * pow2 104 - (b / pow2 52) * pow2 156 + (c % pow2 52) * pow2 156 +
d * pow2 208;
( == ) { lemma_a_mod_52_mul_b c 156 }
t0 + t1 * pow2 52 + t2 * pow2 104 - (b / pow2 52) * pow2 156 + (t3 + b / pow2 52) * pow2 156 -
(c / pow2 52) * pow2 208 +
d * pow2 208;
( == ) { Math.Lemmas.distributivity_add_left t3 (b / pow2 52) (pow2 156) }
t0 + t1 * pow2 52 + t2 * pow2 104 + t3 * pow2 156 - (c / pow2 52) * pow2 208 +
(t4 + c / pow2 52) * pow2 208;
( == ) { Math.Lemmas.distributivity_add_left t4 (c / pow2 52) (pow2 208) }
t0 + t1 * pow2 52 + t2 * pow2 104 + t3 * pow2 156 + t4 * pow2 208;
} | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.on_sort_binder | val on_sort_binder (f: (term -> Tac unit)) (b: binder) : Tac unit | val on_sort_binder (f: (term -> Tac unit)) (b: binder) : Tac unit | let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 10,
"end_line": 653,
"start_col": 0,
"start_line": 652
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
f: (_: FStar.Tactics.NamedView.term -> FStar.Tactics.Effect.Tac Prims.unit) ->
b: FStar.Tactics.NamedView.binder
-> FStar.Tactics.Effect.Tac Prims.unit | FStar.Tactics.Effect.Tac | [] | [] | [
"FStar.Tactics.NamedView.term",
"Prims.unit",
"FStar.Tactics.NamedView.binder",
"FStar.Tactics.NamedView.__proj__Mkbinder__item__sort"
] | [] | false | true | false | false | false | let on_sort_binder (f: (term -> Tac unit)) (b: binder) : Tac unit =
| f b.sort | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.to_vprop' | val to_vprop' : p: Steel.Memory.slprop -> Steel.Effect.Common.vprop' | let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()} | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 64,
"end_line": 99,
"start_col": 0,
"start_line": 99
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *) | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Steel.Memory.slprop -> Steel.Effect.Common.vprop' | Prims.Tot | [
"total"
] | [] | [
"Steel.Memory.slprop",
"Steel.Effect.Common.Mkvprop'",
"Prims.unit",
"Steel.Memory.hmem",
"Steel.Effect.Common.vprop'"
] | [] | false | false | false | true | false | let to_vprop' (p: slprop) =
| { hp = p; t = unit; sel = fun _ -> () } | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.sel_of | val sel_of (p: vprop) : GTot (selector (t_of p) (hp_of p)) | val sel_of (p: vprop) : GTot (selector (t_of p) (hp_of p)) | let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 30,
"end_line": 145,
"start_col": 0,
"start_line": 140
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Steel.Effect.Common.vprop
-> Prims.GTot
(Steel.Effect.Common.selector (Steel.Effect.Common.t_of p) (Steel.Effect.Common.hp_of p)) | Prims.GTot | [
"sometrivial"
] | [] | [
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.vprop'",
"Steel.Memory.hmem",
"Steel.Effect.Common.__proj__Mkvprop'__item__hp",
"Steel.Effect.Common.__proj__Mkvprop'__item__sel",
"Steel.Effect.Common.__proj__Mkvprop'__item__t",
"Steel.Memory.mem",
"Prims.l_and",
"Steel.Memory.interp",
"Steel.Effect.Common.hp_of",
"FStar.Pervasives.Native.Mktuple2",
"Steel.Effect.Common.t_of",
"FStar.Pervasives.Native.tuple2",
"Steel.Effect.Common.selector",
"Steel.Effect.Common.sel_of"
] | [
"recursion"
] | false | false | false | false | false | let rec sel_of (p: vprop) : GTot (selector (t_of p) (hp_of p)) =
| match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h) | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.vdep | val vdep : v: Steel.Effect.Common.vprop -> p: (_: Steel.Effect.Common.t_of v -> Steel.Effect.Common.vprop)
-> Steel.Effect.Common.vprop | let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 69,
"end_line": 621,
"start_col": 0,
"start_line": 621
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
} | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | v: Steel.Effect.Common.vprop -> p: (_: Steel.Effect.Common.t_of v -> Steel.Effect.Common.vprop)
-> Steel.Effect.Common.vprop | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.t_of",
"Steel.Effect.Common.VUnit",
"Steel.Effect.Common.vdep'"
] | [] | false | false | false | false | false | let vdep (v: vprop) (p: ((t_of v) -> Tot vprop)) =
| VUnit (vdep' v p) | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.vrefine' | val vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' | val vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' | let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
} | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 1,
"end_line": 573,
"start_col": 0,
"start_line": 569
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
v: Steel.Effect.Common.vprop ->
p: (_: Steel.Effect.Common.normal (Steel.Effect.Common.t_of v) -> Prims.prop)
-> Steel.Effect.Common.vprop' | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.normal",
"Steel.Effect.Common.t_of",
"Prims.prop",
"Steel.Effect.Common.Mkvprop'",
"Steel.Effect.Common.vrefine_hp",
"Steel.Effect.Common.vrefine_t",
"Steel.Effect.Common.vrefine_sel",
"Steel.Effect.Common.vprop'"
] | [] | false | false | false | false | false | let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' =
| { hp = vrefine_hp v p; t = vrefine_t v p; sel = vrefine_sel v p } | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.term_appears_in | val term_appears_in (t i: term) : Tac bool | val term_appears_in (t i: term) : Tac bool | let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 51,
"end_line": 735,
"start_col": 0,
"start_line": 734
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | t: FStar.Tactics.NamedView.term -> i: FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac Prims.bool | FStar.Tactics.Effect.Tac | [] | [] | [
"FStar.Tactics.NamedView.term",
"Steel.Effect.Common.name_appears_in",
"Prims.bool",
"FStar.Stubs.Reflection.Types.name",
"FStar.Stubs.Reflection.V2.Builtins.explode_qn",
"Prims.string",
"FStar.Stubs.Tactics.V2.Builtins.term_to_string"
] | [] | false | true | false | false | false | let term_appears_in (t i: term) : Tac bool =
| name_appears_in (explode_qn (term_to_string t)) i | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.mk_rmem | val mk_rmem (r: vprop) (h: hmem r) : Tot (rmem r) | val mk_rmem (r: vprop) (h: hmem r) : Tot (rmem r) | let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 14,
"end_line": 306,
"start_col": 0,
"start_line": 304
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__] | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | r: Steel.Effect.Common.vprop -> h: Steel.Effect.Common.hmem r -> Steel.Effect.Common.rmem r | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.hmem",
"Steel.Effect.Common.mk_rmem'",
"Prims.unit",
"Steel.Effect.Common.lemma_valid_mk_rmem",
"Steel.Effect.Common.rmem"
] | [] | false | false | false | false | false | let mk_rmem (r: vprop) (h: hmem r) : Tot (rmem r) =
| lemma_valid_mk_rmem r h;
mk_rmem' r h | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.can_be_split_forall_dep_congr_r | val can_be_split_forall_dep_congr_r (#a: Type) (cond: (a -> prop)) (p q r: post_t a)
: Lemma (requires (can_be_split_forall_dep cond p q))
(ensures
(can_be_split_forall_dep cond (fun x -> (r x) `star` (p x)) (fun x -> (r x) `star` (q x)))) | val can_be_split_forall_dep_congr_r (#a: Type) (cond: (a -> prop)) (p q r: post_t a)
: Lemma (requires (can_be_split_forall_dep cond p q))
(ensures
(can_be_split_forall_dep cond (fun x -> (r x) `star` (p x)) (fun x -> (r x) `star` (q x)))) | let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 94,
"end_line": 253,
"start_col": 0,
"start_line": 245
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z) | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
cond: (_: a -> Prims.prop) ->
p: Steel.Effect.Common.post_t a ->
q: Steel.Effect.Common.post_t a ->
r: Steel.Effect.Common.post_t a
-> FStar.Pervasives.Lemma (requires Steel.Effect.Common.can_be_split_forall_dep cond p q)
(ensures
Steel.Effect.Common.can_be_split_forall_dep cond
(fun x -> Steel.Effect.Common.star (r x) (p x))
(fun x -> Steel.Effect.Common.star (r x) (q x))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.prop",
"Steel.Effect.Common.post_t",
"FStar.Classical.forall_intro_3",
"Steel.Effect.Common.vprop",
"Prims.l_imp",
"Steel.Effect.Common.can_be_split",
"Steel.Effect.Common.star",
"FStar.Classical.move_requires",
"Steel.Effect.Common.can_be_split_congr_r",
"Prims.unit",
"Steel.Effect.Common.can_be_split_forall_dep",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let can_be_split_forall_dep_congr_r (#a: Type) (cond: (a -> prop)) (p q r: post_t a)
: Lemma (requires (can_be_split_forall_dep cond p q))
(ensures
(can_be_split_forall_dep cond (fun x -> (r x) `star` (p x)) (fun x -> (r x) `star` (q x)))) =
| Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z) | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.vdep' | val vdep' (v: vprop) (p: ((t_of v) -> Tot vprop)) : Tot vprop' | val vdep' (v: vprop) (p: ((t_of v) -> Tot vprop)) : Tot vprop' | let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
} | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 1,
"end_line": 618,
"start_col": 0,
"start_line": 614
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | v: Steel.Effect.Common.vprop -> p: (_: Steel.Effect.Common.t_of v -> Steel.Effect.Common.vprop)
-> Steel.Effect.Common.vprop' | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.t_of",
"Steel.Effect.Common.Mkvprop'",
"Steel.Effect.Common.vdep_hp",
"Steel.Effect.Common.vdep_t",
"Steel.Effect.Common.vdep_sel",
"Steel.Effect.Common.vprop'"
] | [] | false | false | false | false | false | let vdep' (v: vprop) (p: ((t_of v) -> Tot vprop)) : Tot vprop' =
| { hp = vdep_hp v p; t = vdep_t v p; sel = vdep_sel v p } | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.can_be_split_forall_dep_congr_l | val can_be_split_forall_dep_congr_l (#a: Type) (cond: (a -> prop)) (p q r: post_t a)
: Lemma (requires (can_be_split_forall_dep cond p q))
(ensures
(can_be_split_forall_dep cond (fun x -> (p x) `star` (r x)) (fun x -> (q x) `star` (r x)))) | val can_be_split_forall_dep_congr_l (#a: Type) (cond: (a -> prop)) (p q r: post_t a)
: Lemma (requires (can_be_split_forall_dep cond p q))
(ensures
(can_be_split_forall_dep cond (fun x -> (p x) `star` (r x)) (fun x -> (q x) `star` (r x)))) | let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 94,
"end_line": 243,
"start_col": 0,
"start_line": 235
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z) | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
cond: (_: a -> Prims.prop) ->
p: Steel.Effect.Common.post_t a ->
q: Steel.Effect.Common.post_t a ->
r: Steel.Effect.Common.post_t a
-> FStar.Pervasives.Lemma (requires Steel.Effect.Common.can_be_split_forall_dep cond p q)
(ensures
Steel.Effect.Common.can_be_split_forall_dep cond
(fun x -> Steel.Effect.Common.star (p x) (r x))
(fun x -> Steel.Effect.Common.star (q x) (r x))) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.prop",
"Steel.Effect.Common.post_t",
"FStar.Classical.forall_intro_3",
"Steel.Effect.Common.vprop",
"Prims.l_imp",
"Steel.Effect.Common.can_be_split",
"Steel.Effect.Common.star",
"FStar.Classical.move_requires",
"Steel.Effect.Common.can_be_split_congr_l",
"Prims.unit",
"Steel.Effect.Common.can_be_split_forall_dep",
"Prims.squash",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let can_be_split_forall_dep_congr_l (#a: Type) (cond: (a -> prop)) (p q r: post_t a)
: Lemma (requires (can_be_split_forall_dep cond p q))
(ensures
(can_be_split_forall_dep cond (fun x -> (p x) `star` (r x)) (fun x -> (q x) `star` (r x)))) =
| Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z) | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.name_appears_in | val name_appears_in (nm: name) (t: term) : Tac bool | val name_appears_in (nm: name) (t: term) : Tac bool | let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 16,
"end_line": 731,
"start_col": 0,
"start_line": 723
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | nm: FStar.Stubs.Reflection.Types.name -> t: FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac Prims.bool | FStar.Tactics.Effect.Tac | [] | [] | [
"FStar.Stubs.Reflection.Types.name",
"FStar.Tactics.NamedView.term",
"FStar.Tactics.V2.Derived.try_with",
"Prims.bool",
"Prims.unit",
"FStar.Pervasives.ignore",
"Steel.Effect.Common.visit_tm",
"Prims.exn",
"FStar.Tactics.Effect.raise",
"FStar.Stubs.Reflection.Types.fv",
"Prims.op_Equality",
"FStar.Stubs.Reflection.V2.Builtins.inspect_fv",
"Steel.Effect.Common.Appears",
"FStar.Tactics.NamedView.named_term_view",
"FStar.Tactics.NamedView.inspect"
] | [] | false | true | false | false | false | let name_appears_in (nm: name) (t: term) : Tac bool =
| let ff (t: term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try
(ignore (visit_tm ff t);
false)
with
| Appears -> true
| e -> raise e | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.unrestricted_mk_rmem | val unrestricted_mk_rmem : r: Steel.Effect.Common.vprop ->
h: Steel.Effect.Common.hmem r ->
r0: Steel.Effect.Common.vprop{Steel.Effect.Common.can_be_split r r0}
-> Prims.GTot (Steel.Effect.Common.t_of r0) | let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 13,
"end_line": 291,
"start_col": 0,
"start_line": 289
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
r: Steel.Effect.Common.vprop ->
h: Steel.Effect.Common.hmem r ->
r0: Steel.Effect.Common.vprop{Steel.Effect.Common.can_be_split r r0}
-> Prims.GTot (Steel.Effect.Common.t_of r0) | Prims.GTot | [
"sometrivial"
] | [] | [
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.hmem",
"Steel.Effect.Common.can_be_split",
"Steel.Effect.Common.sel_of",
"Prims.unit",
"Steel.Effect.Common.can_be_split_interp",
"Steel.Effect.Common.t_of"
] | [] | false | false | false | false | false | let unrestricted_mk_rmem (r: vprop) (h: hmem r) =
| fun (r0: vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.t_of | val t_of : p: Steel.Effect.Common.vprop -> Type0 | let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2 | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 36,
"end_line": 136,
"start_col": 0,
"start_line": 134
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | p: Steel.Effect.Common.vprop -> Type0 | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.vprop'",
"Steel.Effect.Common.__proj__Mkvprop'__item__t",
"FStar.Pervasives.Native.tuple2",
"Steel.Effect.Common.t_of"
] | [
"recursion"
] | false | false | false | true | true | let rec t_of (p: vprop) =
| match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2 | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.const | val const (#a: Type) (xa: a) : amap a | val const (#a: Type) (xa: a) : amap a | let const (#a:Type) (xa:a) : amap a = ([], xa) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 46,
"end_line": 759,
"start_col": 0,
"start_line": 759
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | xa: a -> Steel.Effect.Common.amap a | Prims.Tot | [
"total"
] | [] | [
"FStar.Pervasives.Native.Mktuple2",
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"Steel.Effect.Common.atom",
"Prims.Nil",
"Steel.Effect.Common.amap"
] | [] | false | false | false | true | false | let const (#a: Type) (xa: a) : amap a =
| ([], xa) | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.amap | val amap : a: Type -> Type | let amap (a:Type) = list (atom * a) * a | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 39,
"end_line": 756,
"start_col": 0,
"start_line": 756
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element, | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | a: Type -> Type | Prims.Tot | [
"total"
] | [] | [
"FStar.Pervasives.Native.tuple2",
"Prims.list",
"Steel.Effect.Common.atom"
] | [] | false | false | false | true | true | let amap (a: Type) =
| list (atom * a) * a | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.select | val select (#a: Type) (x: atom) (am: amap a) : Tot a | val select (#a: Type) (x: atom) (am: amap a) : Tot a | let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 15,
"end_line": 776,
"start_col": 0,
"start_line": 773
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Steel.Effect.Common.atom -> am: Steel.Effect.Common.amap a -> a | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.atom",
"Steel.Effect.Common.amap",
"Steel.Effect.Common.my_assoc",
"FStar.Pervasives.Native.fst",
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"FStar.Pervasives.Native.option",
"FStar.Pervasives.Native.snd"
] | [] | false | false | false | true | false | let select (#a: Type) (x: atom) (am: amap a) : Tot a =
| match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.can_be_split_forall_dep_trans_rev | val can_be_split_forall_dep_trans_rev (#a: Type) (cond1 cond2: (a -> prop)) (p q r: post_t a)
: Lemma (requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> (cond1 x) `prop_and` (cond2 x)) p r)) | val can_be_split_forall_dep_trans_rev (#a: Type) (cond1 cond2: (a -> prop)) (p q r: post_t a)
: Lemma (requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> (cond1 x) `prop_and` (cond2 x)) p r)) | let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 92,
"end_line": 233,
"start_col": 0,
"start_line": 225
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2 | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
cond1: (_: a -> Prims.prop) ->
cond2: (_: a -> Prims.prop) ->
p: Steel.Effect.Common.post_t a ->
q: Steel.Effect.Common.post_t a ->
r: Steel.Effect.Common.post_t a
-> FStar.Pervasives.Lemma
(requires
Steel.Effect.Common.can_be_split_forall_dep cond2 q r /\
Steel.Effect.Common.can_be_split_forall_dep cond1 p q)
(ensures
Steel.Effect.Common.can_be_split_forall_dep (fun x ->
Steel.Effect.Common.prop_and (cond1 x) (cond2 x))
p
r) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Prims.prop",
"Steel.Effect.Common.post_t",
"FStar.Classical.forall_intro_3",
"Steel.Effect.Common.vprop",
"Prims.l_imp",
"Prims.l_and",
"Steel.Effect.Common.can_be_split",
"FStar.Classical.move_requires",
"Steel.Effect.Common.can_be_split_trans",
"Prims.unit",
"Steel.Effect.Common.can_be_split_forall_dep",
"Prims.squash",
"Steel.Effect.Common.prop_and",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let can_be_split_forall_dep_trans_rev (#a: Type) (cond1 cond2: (a -> prop)) (p q r: post_t a)
: Lemma (requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> (cond1 x) `prop_and` (cond2 x)) p r)) =
| Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z) | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.update | val update (#a: Type) (x: atom) (xa: a) (am: amap a) : amap a | val update (#a: Type) (x: atom) (xa: a) (am: amap a) : amap a | let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 25,
"end_line": 783,
"start_col": 0,
"start_line": 782
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: Steel.Effect.Common.atom -> xa: a -> am: Steel.Effect.Common.amap a -> Steel.Effect.Common.amap a | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.atom",
"Steel.Effect.Common.amap",
"FStar.Pervasives.Native.Mktuple2",
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"Prims.Cons",
"FStar.Pervasives.Native.fst",
"FStar.Pervasives.Native.snd"
] | [] | false | false | false | true | false | let update (#a: Type) (x: atom) (xa: a) (am: amap a) : amap a =
| (x, xa) :: fst am, snd am | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.focus_rmem | val focus_rmem (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem r0) | val focus_rmem (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem r0) | let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0 | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 18,
"end_line": 356,
"start_col": 0,
"start_line": 354
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0)) | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
h: Steel.Effect.Common.rmem r ->
r0: Steel.Effect.Common.vprop{Steel.Effect.Common.can_be_split r r0}
-> Steel.Effect.Common.rmem r0 | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.rmem",
"Steel.Effect.Common.can_be_split",
"Steel.Effect.Common.focus_rmem'",
"Prims.unit",
"Steel.Effect.Common.lemma_valid_focus_rmem"
] | [] | false | false | false | false | false | let focus_rmem (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem r0) =
| lemma_valid_focus_rmem h r0;
focus_rmem' h r0 | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.focus_rmem_refl | val focus_rmem_refl (r: vprop) (h: rmem r) : Lemma (focus_rmem #r h r == h) | val focus_rmem_refl (r: vprop) (h: rmem r) : Lemma (focus_rmem #r h r == h) | let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 77,
"end_line": 361,
"start_col": 0,
"start_line": 359
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0 | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | r: Steel.Effect.Common.vprop -> h: Steel.Effect.Common.rmem r
-> FStar.Pervasives.Lemma (ensures Steel.Effect.Common.focus_rmem h r == h) | FStar.Pervasives.Lemma | [
"lemma"
] | [] | [
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.rmem",
"FStar.FunctionalExtensionality.extensionality_g",
"Steel.Effect.Common.can_be_split",
"Steel.Effect.Common.normal",
"Steel.Effect.Common.t_of",
"Steel.Effect.Common.focus_rmem",
"Prims.unit",
"Prims.l_True",
"Prims.squash",
"Prims.eq2",
"Prims.Nil",
"FStar.Pervasives.pattern"
] | [] | false | false | true | false | false | let focus_rmem_refl (r: vprop) (h: rmem r) : Lemma (focus_rmem #r h r == h) =
| FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.atom | val atom:eqtype | val atom:eqtype | let atom : eqtype = int | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 23,
"end_line": 741,
"start_col": 0,
"start_line": 741
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable, | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.eqtype | Prims.Tot | [
"total"
] | [] | [
"Prims.int"
] | [] | false | false | false | true | false | let atom:eqtype =
| int | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.frame_equalities' | val frame_equalities' (frame: vprop) (h0 h1: rmem frame) : Type0 | val frame_equalities' (frame: vprop) (h0 h1: rmem frame) : Type0 | let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 7,
"end_line": 390,
"start_col": 0,
"start_line": 372
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
frame: Steel.Effect.Common.vprop ->
h0: Steel.Effect.Common.rmem frame ->
h1: Steel.Effect.Common.rmem frame
-> Type0 | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.rmem",
"Steel.Effect.Common.vprop'",
"Prims.eq2",
"Steel.Effect.Common.normal",
"Steel.Effect.Common.t_of",
"Prims.l_and",
"Steel.Effect.Common.frame_equalities'",
"Steel.Effect.Common.rmem'",
"Steel.Effect.Common.valid_rmem",
"Steel.Effect.Common.focus_rmem",
"Prims.unit",
"Steel.Effect.Common.can_be_split_star_r",
"Steel.Effect.Common.can_be_split_star_l"
] | [
"recursion"
] | false | false | false | false | true | let rec frame_equalities' (frame: vprop) (h0 h1: rmem frame) : Type0 =
| match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\ frame_equalities' p2 h02 h12 | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.is_uvar | val is_uvar (t: term) : Tac bool | val is_uvar (t: term) : Tac bool | let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 14,
"end_line": 793,
"start_col": 0,
"start_line": 788
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | t: FStar.Tactics.NamedView.term -> FStar.Tactics.Effect.Tac Prims.bool | FStar.Tactics.Effect.Tac | [] | [] | [
"FStar.Tactics.NamedView.term",
"Prims.nat",
"FStar.Stubs.Reflection.Types.ctx_uvar_and_subst",
"Prims.bool",
"FStar.Stubs.Reflection.V2.Data.argv",
"Prims.list",
"FStar.Tactics.NamedView.uu___is_Tv_Uvar",
"FStar.Tactics.NamedView.named_term_view",
"FStar.Tactics.NamedView.inspect",
"FStar.Pervasives.Native.tuple2",
"FStar.Tactics.V2.SyntaxHelpers.collect_app"
] | [] | false | true | false | false | false | let is_uvar (t: term) : Tac bool =
| match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.mk_rmem' | val mk_rmem' (r: vprop) (h: hmem r) : Tot (rmem' r) | val mk_rmem' (r: vprop) (h: hmem r) : Tot (rmem' r) | let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 31,
"end_line": 298,
"start_col": 0,
"start_line": 295
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__] | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | r: Steel.Effect.Common.vprop -> h: Steel.Effect.Common.hmem r -> Steel.Effect.Common.rmem' r | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.hmem",
"FStar.FunctionalExtensionality.on_dom_g",
"Steel.Effect.Common.can_be_split",
"Steel.Effect.Common.normal",
"Steel.Effect.Common.t_of",
"Steel.Effect.Common.unrestricted_mk_rmem",
"Steel.Effect.Common.rmem'"
] | [] | false | false | false | false | false | let mk_rmem' (r: vprop) (h: hmem r) : Tot (rmem' r) =
| FExt.on_dom_g (r0: vprop{r `can_be_split` r0}) (unrestricted_mk_rmem r h) | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.rm | val rm:CE.cm vprop req | val rm:CE.cm vprop req | let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 20,
"end_line": 538,
"start_col": 32,
"start_line": 532
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | FStar.Algebra.CommMonoid.Equiv.cm Steel.Effect.Common.vprop Steel.Effect.Common.req | Prims.Tot | [
"total"
] | [] | [
"FStar.Algebra.CommMonoid.Equiv.CM",
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.req",
"Steel.Effect.Common.emp",
"Steel.Effect.Common.star",
"Steel.Effect.Common.cm_identity",
"Steel.Effect.Common.star_associative",
"Steel.Effect.Common.star_commutative",
"Steel.Effect.Common.star_congruence"
] | [] | false | false | false | true | false | let rm:CE.cm vprop req =
| CE.CM emp star cm_identity star_associative star_commutative star_congruence | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.unrestricted_focus_rmem | val unrestricted_focus_rmem : h: Steel.Effect.Common.rmem r ->
r0: Steel.Effect.Common.vprop{Steel.Effect.Common.can_be_split r r0} ->
r': Steel.Effect.Common.vprop{Steel.Effect.Common.can_be_split r0 r'}
-> Prims.GTot (Steel.Effect.Common.normal (Steel.Effect.Common.t_of r')) | let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r' | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 74,
"end_line": 342,
"start_col": 0,
"start_line": 341
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__] | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
h: Steel.Effect.Common.rmem r ->
r0: Steel.Effect.Common.vprop{Steel.Effect.Common.can_be_split r r0} ->
r': Steel.Effect.Common.vprop{Steel.Effect.Common.can_be_split r0 r'}
-> Prims.GTot (Steel.Effect.Common.normal (Steel.Effect.Common.t_of r')) | Prims.GTot | [
"sometrivial"
] | [] | [
"Steel.Effect.Common.vprop",
"Steel.Effect.Common.rmem",
"Steel.Effect.Common.can_be_split",
"Prims.unit",
"Steel.Effect.Common.can_be_split_trans",
"Steel.Effect.Common.normal",
"Steel.Effect.Common.t_of"
] | [] | false | false | false | false | false | let unrestricted_focus_rmem (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) =
| fun (r': vprop{can_be_split r0 r'}) ->
can_be_split_trans r r0 r';
h r' | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.is_smt_binder | val is_smt_binder (b: binder) : Tac bool | val is_smt_binder (b: binder) : Tac bool | let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l)) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 73,
"end_line": 932,
"start_col": 0,
"start_line": 930
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am)) | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | b: FStar.Tactics.NamedView.binder -> FStar.Tactics.Effect.Tac Prims.bool | FStar.Tactics.Effect.Tac | [] | [] | [
"FStar.Tactics.NamedView.binder",
"Prims.op_Negation",
"Prims.bool",
"FStar.List.Tot.Base.isEmpty",
"FStar.Stubs.Reflection.Types.term",
"Prims.list",
"FStar.Tactics.Util.filter",
"FStar.Reflection.V2.Derived.is_fvar",
"FStar.Tactics.NamedView.term",
"FStar.Tactics.NamedView.__proj__Mkbinder__item__attrs"
] | [] | false | true | false | false | false | let is_smt_binder (b: binder) : Tac bool =
| let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l)) | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.xsdenote | val xsdenote (#a: Type) (eq: CE.equiv a) (m: CE.cm a eq) (am: amap a) (xs: list atom) : a | val xsdenote (#a: Type) (eq: CE.equiv a) (m: CE.cm a eq) (am: amap a) (xs: list atom) : a | let xsdenote (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom) : a =
let open FStar.Algebra.CommMonoid.Equiv in
xsdenote_gen (CM?.unit m) (CM?.mult m) am xs | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 46,
"end_line": 1135,
"start_col": 0,
"start_line": 1133
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am
/// Iterates over all terms in [l2] to prepare them for unification with SMT rewriting
let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2
/// Recursively calls equivalent_lists_once.
/// Stops when we're done with unification, or when we didn't make any progress
/// If we didn't make any progress, we have too many candidates for some terms.
/// Accumulates rewritings of l1 and l2 in l1_del and l2_del, with the invariant
/// that the two lists are unifiable at any point
/// The boolean indicates if there is a leftover empty frame
let rec equivalent_lists' (n:nat) (use_smt:bool) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, []
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Try to make progress by matching non-uvars of l2 with candidates in l1
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Should always be smaller or equal to n
// If it is equal, no progress was made.
if use_smt then
// SMT fallback is allowed
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
/// Checks if term for atom t unifies with fall uvars in l
let rec unifies_with_all_uvars (t:term) (l:list atom) (am:amap term) : Tac bool =
match l with
| [] -> true
| hd::tl ->
if unifies_with_all_uvars t tl am then (
// Unified with tail, try this term
let hd_t = select hd am in
if is_uvar hd_t then (
// The head term is a uvar, try unifying
try if unify t hd_t then raise Success else raise Failed
with | Success -> true | _ -> false
) else true // The uvar is not a head term, we do not need to try it
) else false
/// Puts all terms in l1 that cannot unify with the uvars in l2 at the top:
/// They need to be solved first
let rec most_restricted_at_top (l1 l2:list atom) (am:amap term) : Tac (list atom) =
match l1 with
| [] -> []
| hd::tl ->
if unifies_with_all_uvars (select hd am) l2 am then (most_restricted_at_top tl l2 am) `List.Tot.append` [hd]
else hd::(most_restricted_at_top tl l2 am)
/// Core AC-unification tactic.
/// First remove all trivially equal terms, then try to decide equivalence.
/// Assumes that l1 does not contain any vprop uvar.
/// If it succeeds, returns permutations of l1, l2, and a boolean indicating
/// if l2 has a trailing empty frame to be unified
let equivalent_lists (use_smt:bool) (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term)
= let l1, l2, l1_del, l2_del = trivial_cancels l1 l2 am in
let l1 = most_restricted_at_top l1 l2 am in
let n = List.Tot.length l1 in
let l1_del, l2_del, emp_frame, uvar_terms = equivalent_lists' n use_smt l1 l2 l1_del l2_del am in
l1_del, l2_del, emp_frame, uvar_terms
(* Helpers to relate the actual terms to their representation as a list of atoms *)
open FStar.Reflection.V2.Derived.Lemmas
let rec list_to_string (l:list term) : Tac string =
match l with
| [] -> "end"
| hd::tl -> term_to_string hd ^ " " ^ list_to_string tl
let rec mdenote_gen (#a:Type u#aa) (unit:a) (mult:a -> a -> a) (am:amap a) (e:exp) : a =
match e with
| Unit -> unit
| Atom x -> select x am
| Mult e1 e2 -> mult (mdenote_gen unit mult am e1) (mdenote_gen unit mult am e2)
let rec xsdenote_gen (#a:Type) (unit:a) (mult:a -> a -> a) (am:amap a) (xs:list atom) : a =
match xs with
| [] -> unit
| [x] -> select x am
| x::xs' -> mult (select x am) (xsdenote_gen unit mult am xs')
unfold
let mdenote (#a:Type u#aa) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp) : a =
let open FStar.Algebra.CommMonoid.Equiv in
mdenote_gen (CM?.unit m) (CM?.mult m) am e | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Reflection.V2.Derived.Lemmas",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
eq: FStar.Algebra.CommMonoid.Equiv.equiv a ->
m: FStar.Algebra.CommMonoid.Equiv.cm a eq ->
am: Steel.Effect.Common.amap a ->
xs: Prims.list Steel.Effect.Common.atom
-> a | Prims.Tot | [
"total"
] | [] | [
"FStar.Algebra.CommMonoid.Equiv.equiv",
"FStar.Algebra.CommMonoid.Equiv.cm",
"Steel.Effect.Common.amap",
"Prims.list",
"Steel.Effect.Common.atom",
"Steel.Effect.Common.xsdenote_gen",
"FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit",
"FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult"
] | [] | false | false | false | false | false | let xsdenote (#a: Type) (eq: CE.equiv a) (m: CE.cm a eq) (am: amap a) (xs: list atom) : a =
| let open FStar.Algebra.CommMonoid.Equiv in xsdenote_gen (CM?.unit m) (CM?.mult m) am xs | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.fail_atoms | val fail_atoms (#a: Type) (l1 l2: list atom) (am: amap term) : Tac a | val fail_atoms (#a: Type) (l1 l2: list atom) (am: amap term) : Tac a | let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 105,
"end_line": 981,
"start_col": 0,
"start_line": 980
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
l1: Prims.list Steel.Effect.Common.atom ->
l2: Prims.list Steel.Effect.Common.atom ->
am: Steel.Effect.Common.amap FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac a | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.list",
"Steel.Effect.Common.atom",
"Steel.Effect.Common.amap",
"FStar.Tactics.NamedView.term",
"FStar.Tactics.V2.Derived.fail",
"Prims.string",
"Prims.op_Hat",
"Steel.Effect.Common.print_atoms"
] | [] | false | true | false | false | false | let fail_atoms (#a: Type) (l1 l2: list atom) (am: amap term) : Tac a =
| fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am
) | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.mdenote | val mdenote (#a: Type u#aa) (eq: CE.equiv a) (m: CE.cm a eq) (am: amap a) (e: exp) : a | val mdenote (#a: Type u#aa) (eq: CE.equiv a) (m: CE.cm a eq) (am: amap a) (e: exp) : a | let mdenote (#a:Type u#aa) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp) : a =
let open FStar.Algebra.CommMonoid.Equiv in
mdenote_gen (CM?.unit m) (CM?.mult m) am e | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 44,
"end_line": 1130,
"start_col": 0,
"start_line": 1128
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am
/// Iterates over all terms in [l2] to prepare them for unification with SMT rewriting
let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2
/// Recursively calls equivalent_lists_once.
/// Stops when we're done with unification, or when we didn't make any progress
/// If we didn't make any progress, we have too many candidates for some terms.
/// Accumulates rewritings of l1 and l2 in l1_del and l2_del, with the invariant
/// that the two lists are unifiable at any point
/// The boolean indicates if there is a leftover empty frame
let rec equivalent_lists' (n:nat) (use_smt:bool) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, []
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Try to make progress by matching non-uvars of l2 with candidates in l1
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Should always be smaller or equal to n
// If it is equal, no progress was made.
if use_smt then
// SMT fallback is allowed
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
/// Checks if term for atom t unifies with fall uvars in l
let rec unifies_with_all_uvars (t:term) (l:list atom) (am:amap term) : Tac bool =
match l with
| [] -> true
| hd::tl ->
if unifies_with_all_uvars t tl am then (
// Unified with tail, try this term
let hd_t = select hd am in
if is_uvar hd_t then (
// The head term is a uvar, try unifying
try if unify t hd_t then raise Success else raise Failed
with | Success -> true | _ -> false
) else true // The uvar is not a head term, we do not need to try it
) else false
/// Puts all terms in l1 that cannot unify with the uvars in l2 at the top:
/// They need to be solved first
let rec most_restricted_at_top (l1 l2:list atom) (am:amap term) : Tac (list atom) =
match l1 with
| [] -> []
| hd::tl ->
if unifies_with_all_uvars (select hd am) l2 am then (most_restricted_at_top tl l2 am) `List.Tot.append` [hd]
else hd::(most_restricted_at_top tl l2 am)
/// Core AC-unification tactic.
/// First remove all trivially equal terms, then try to decide equivalence.
/// Assumes that l1 does not contain any vprop uvar.
/// If it succeeds, returns permutations of l1, l2, and a boolean indicating
/// if l2 has a trailing empty frame to be unified
let equivalent_lists (use_smt:bool) (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term)
= let l1, l2, l1_del, l2_del = trivial_cancels l1 l2 am in
let l1 = most_restricted_at_top l1 l2 am in
let n = List.Tot.length l1 in
let l1_del, l2_del, emp_frame, uvar_terms = equivalent_lists' n use_smt l1 l2 l1_del l2_del am in
l1_del, l2_del, emp_frame, uvar_terms
(* Helpers to relate the actual terms to their representation as a list of atoms *)
open FStar.Reflection.V2.Derived.Lemmas
let rec list_to_string (l:list term) : Tac string =
match l with
| [] -> "end"
| hd::tl -> term_to_string hd ^ " " ^ list_to_string tl
let rec mdenote_gen (#a:Type u#aa) (unit:a) (mult:a -> a -> a) (am:amap a) (e:exp) : a =
match e with
| Unit -> unit
| Atom x -> select x am
| Mult e1 e2 -> mult (mdenote_gen unit mult am e1) (mdenote_gen unit mult am e2)
let rec xsdenote_gen (#a:Type) (unit:a) (mult:a -> a -> a) (am:amap a) (xs:list atom) : a =
match xs with
| [] -> unit
| [x] -> select x am
| x::xs' -> mult (select x am) (xsdenote_gen unit mult am xs') | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Reflection.V2.Derived.Lemmas",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
eq: FStar.Algebra.CommMonoid.Equiv.equiv a ->
m: FStar.Algebra.CommMonoid.Equiv.cm a eq ->
am: Steel.Effect.Common.amap a ->
e: Steel.Effect.Common.exp
-> a | Prims.Tot | [
"total"
] | [] | [
"FStar.Algebra.CommMonoid.Equiv.equiv",
"FStar.Algebra.CommMonoid.Equiv.cm",
"Steel.Effect.Common.amap",
"Steel.Effect.Common.exp",
"Steel.Effect.Common.mdenote_gen",
"FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit",
"FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult"
] | [] | false | false | false | false | false | let mdenote (#a: Type u#aa) (eq: CE.equiv a) (m: CE.cm a eq) (am: amap a) (e: exp) : a =
| let open FStar.Algebra.CommMonoid.Equiv in mdenote_gen (CM?.unit m) (CM?.mult m) am e | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.replace_smt_uvars | val replace_smt_uvars (l1 l2: list atom) (am: amap term) : Tac (amap term * list term) | val replace_smt_uvars (l1 l2: list atom) (am: amap term) : Tac (amap term * list term) | let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2 | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 52,
"end_line": 1020,
"start_col": 0,
"start_line": 1018
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
l1: Prims.list Steel.Effect.Common.atom ->
l2: Prims.list Steel.Effect.Common.atom ->
am: Steel.Effect.Common.amap FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac
(Steel.Effect.Common.amap FStar.Tactics.NamedView.term * Prims.list FStar.Tactics.NamedView.term
) | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.list",
"Steel.Effect.Common.atom",
"Steel.Effect.Common.amap",
"FStar.Tactics.NamedView.term",
"FStar.Tactics.Util.fold_left",
"FStar.Pervasives.Native.tuple2",
"Steel.Effect.Common.rewrite_term_for_smt",
"FStar.Pervasives.Native.Mktuple2",
"Prims.Nil",
"FStar.Stubs.Reflection.Types.env",
"FStar.Tactics.V2.Derived.cur_env"
] | [] | false | true | false | false | false | let replace_smt_uvars (l1 l2: list atom) (am: amap term) : Tac (amap term * list term) =
| let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2 | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.rewrite_term_for_smt | val rewrite_term_for_smt (env: env) (am: (amap term * list term)) (a: atom)
: Tac (amap term * list term) | val rewrite_term_for_smt (env: env) (am: (amap term * list term)) (a: atom)
: Tac (amap term * list term) | let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 68,
"end_line": 977,
"start_col": 0,
"start_line": 970
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?" | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
env: FStar.Stubs.Reflection.Types.env ->
am:
(Steel.Effect.Common.amap FStar.Tactics.NamedView.term *
Prims.list FStar.Tactics.NamedView.term) ->
a: Steel.Effect.Common.atom
-> FStar.Tactics.Effect.Tac
(Steel.Effect.Common.amap FStar.Tactics.NamedView.term * Prims.list FStar.Tactics.NamedView.term
) | FStar.Tactics.Effect.Tac | [] | [] | [
"FStar.Stubs.Reflection.Types.env",
"FStar.Pervasives.Native.tuple2",
"Steel.Effect.Common.amap",
"FStar.Tactics.NamedView.term",
"Prims.list",
"Steel.Effect.Common.atom",
"FStar.Stubs.Reflection.V2.Data.argv",
"FStar.Pervasives.Native.Mktuple2",
"Steel.Effect.Common.update",
"FStar.List.Tot.Base.append",
"FStar.Stubs.Reflection.Types.term",
"FStar.Reflection.V2.Derived.mk_app",
"Steel.Effect.Common.new_args_for_smt_attrs",
"FStar.Stubs.Tactics.V2.Builtins.tc",
"FStar.Tactics.V2.SyntaxHelpers.collect_app",
"Steel.Effect.Common.select"
] | [] | false | true | false | false | false | let rewrite_term_for_smt (env: env) (am: amap term * list term) (a: atom)
: Tac (amap term * list term) =
| let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.atoms_to_string | val atoms_to_string : l: Prims.list Steel.Effect.Common.atom -> Prims.string | let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 57,
"end_line": 745,
"start_col": 0,
"start_line": 743
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list Steel.Effect.Common.atom -> Prims.string | Prims.Tot | [
"total"
] | [] | [
"Prims.list",
"Steel.Effect.Common.atom",
"Prims.op_Hat",
"Prims.string_of_int",
"Steel.Effect.Common.atoms_to_string",
"Prims.string"
] | [
"recursion"
] | false | false | false | true | false | let rec atoms_to_string (l: list atom) =
| match l with
| [] -> ""
| hd :: tl -> string_of_int hd ^ " " ^ atoms_to_string tl | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.equivalent_lists_fallback | val equivalent_lists_fallback (n: nat) (l1 l2 l1_del l2_del: list atom) (am: amap term)
: Tac (list atom * list atom * bool) | val equivalent_lists_fallback (n: nat) (l1 l2 l1_del l2_del: list atom) (am: amap term)
: Tac (list atom * list atom * bool) | let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 68,
"end_line": 1015,
"start_col": 0,
"start_line": 986
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed, | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
n: Prims.nat ->
l1: Prims.list Steel.Effect.Common.atom ->
l2: Prims.list Steel.Effect.Common.atom ->
l1_del: Prims.list Steel.Effect.Common.atom ->
l2_del: Prims.list Steel.Effect.Common.atom ->
am: Steel.Effect.Common.amap FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac
((Prims.list Steel.Effect.Common.atom * Prims.list Steel.Effect.Common.atom) * Prims.bool) | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.nat",
"Prims.list",
"Steel.Effect.Common.atom",
"Steel.Effect.Common.amap",
"FStar.Tactics.NamedView.term",
"FStar.Pervasives.Native.Mktuple3",
"Prims.bool",
"FStar.Pervasives.Native.tuple3",
"Prims.Cons",
"FStar.Tactics.V2.Derived.fail",
"Prims.string",
"Prims.op_Hat",
"FStar.Stubs.Tactics.V2.Builtins.term_to_string",
"Steel.Effect.Common.get_head",
"Steel.Effect.Common.is_uvar",
"Steel.Effect.Common.select",
"FStar.List.Tot.Base.append",
"Prims.unit",
"Steel.Effect.Common.try_unifying_remaining",
"Prims.op_GreaterThanOrEqual",
"Steel.Effect.Common.fail_atoms",
"Steel.Effect.Common.equivalent_lists_fallback",
"FStar.List.Tot.Base.length",
"FStar.Pervasives.Native.tuple4",
"Steel.Effect.Common.equivalent_lists_once",
"Steel.Effect.Common.is_only_uvar"
] | [
"recursion"
] | false | true | false | false | false | let rec equivalent_lists_fallback (n: nat) (l1 l2 l1_del l2_del: list atom) (am: amap term)
: Tac (list atom * list atom * bool) =
| match l1 with
| [] ->
(match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
if is_uvar (select hd am)
then ((l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am)))
| _ ->
if is_only_uvar l2 am
then
(try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false)
else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n
then fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.equivalent_lists | val equivalent_lists (use_smt: bool) (l1 l2: list atom) (am: amap term)
: Tac (list atom * list atom * bool * list term) | val equivalent_lists (use_smt: bool) (l1 l2: list atom) (am: amap term)
: Tac (list atom * list atom * bool * list term) | let equivalent_lists (use_smt:bool) (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term)
= let l1, l2, l1_del, l2_del = trivial_cancels l1 l2 am in
let l1 = most_restricted_at_top l1 l2 am in
let n = List.Tot.length l1 in
let l1_del, l2_del, emp_frame, uvar_terms = equivalent_lists' n use_smt l1 l2 l1_del l2_del am in
l1_del, l2_del, emp_frame, uvar_terms | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 39,
"end_line": 1104,
"start_col": 0,
"start_line": 1098
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am
/// Iterates over all terms in [l2] to prepare them for unification with SMT rewriting
let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2
/// Recursively calls equivalent_lists_once.
/// Stops when we're done with unification, or when we didn't make any progress
/// If we didn't make any progress, we have too many candidates for some terms.
/// Accumulates rewritings of l1 and l2 in l1_del and l2_del, with the invariant
/// that the two lists are unifiable at any point
/// The boolean indicates if there is a leftover empty frame
let rec equivalent_lists' (n:nat) (use_smt:bool) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, []
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Try to make progress by matching non-uvars of l2 with candidates in l1
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Should always be smaller or equal to n
// If it is equal, no progress was made.
if use_smt then
// SMT fallback is allowed
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
/// Checks if term for atom t unifies with fall uvars in l
let rec unifies_with_all_uvars (t:term) (l:list atom) (am:amap term) : Tac bool =
match l with
| [] -> true
| hd::tl ->
if unifies_with_all_uvars t tl am then (
// Unified with tail, try this term
let hd_t = select hd am in
if is_uvar hd_t then (
// The head term is a uvar, try unifying
try if unify t hd_t then raise Success else raise Failed
with | Success -> true | _ -> false
) else true // The uvar is not a head term, we do not need to try it
) else false
/// Puts all terms in l1 that cannot unify with the uvars in l2 at the top:
/// They need to be solved first
let rec most_restricted_at_top (l1 l2:list atom) (am:amap term) : Tac (list atom) =
match l1 with
| [] -> []
| hd::tl ->
if unifies_with_all_uvars (select hd am) l2 am then (most_restricted_at_top tl l2 am) `List.Tot.append` [hd]
else hd::(most_restricted_at_top tl l2 am)
/// Core AC-unification tactic.
/// First remove all trivially equal terms, then try to decide equivalence.
/// Assumes that l1 does not contain any vprop uvar.
/// If it succeeds, returns permutations of l1, l2, and a boolean indicating | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
use_smt: Prims.bool ->
l1: Prims.list Steel.Effect.Common.atom ->
l2: Prims.list Steel.Effect.Common.atom ->
am: Steel.Effect.Common.amap FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac
(((Prims.list Steel.Effect.Common.atom * Prims.list Steel.Effect.Common.atom) * Prims.bool) *
Prims.list FStar.Tactics.NamedView.term) | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.bool",
"Prims.list",
"Steel.Effect.Common.atom",
"Steel.Effect.Common.amap",
"FStar.Tactics.NamedView.term",
"FStar.Pervasives.Native.Mktuple4",
"FStar.Pervasives.Native.tuple4",
"Steel.Effect.Common.equivalent_lists'",
"Prims.nat",
"FStar.List.Tot.Base.length",
"Steel.Effect.Common.most_restricted_at_top",
"Steel.Effect.Common.trivial_cancels"
] | [] | false | true | false | false | false | let equivalent_lists (use_smt: bool) (l1 l2: list atom) (am: amap term)
: Tac (list atom * list atom * bool * list term) =
| let l1, l2, l1_del, l2_del = trivial_cancels l1 l2 am in
let l1 = most_restricted_at_top l1 l2 am in
let n = List.Tot.length l1 in
let l1_del, l2_del, emp_frame, uvar_terms = equivalent_lists' n use_smt l1 l2 l1_del l2_del am in
l1_del, l2_del, emp_frame, uvar_terms | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.my_assoc | val my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value))
: Pure (option value) (requires True) (ensures (fun res -> res == List.Tot.assoc k dict)) | val my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value))
: Pure (option value) (requires True) (ensures (fun res -> res == List.Tot.assoc k dict)) | let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 61,
"end_line": 770,
"start_col": 0,
"start_line": 765
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it, | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | k: key -> dict: Prims.list (key * value) -> Prims.Pure (FStar.Pervasives.Native.option value) | Prims.Pure | [] | [] | [
"Prims.eqtype",
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"FStar.Pervasives.Native.None",
"Prims.op_Equality",
"FStar.Pervasives.Native.Some",
"Prims.bool",
"Steel.Effect.Common.my_assoc",
"FStar.Pervasives.Native.option",
"Prims.l_True",
"Prims.eq2",
"FStar.List.Tot.Base.assoc"
] | [
"recursion"
] | false | false | false | false | false | let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value))
: Pure (option value) (requires True) (ensures (fun res -> res == List.Tot.assoc k dict)) =
| match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.my_bool_of_compare | val my_bool_of_compare (#a: Type) (f: (a -> a -> Tot int)) (x y: a) : Tot bool | val my_bool_of_compare (#a: Type) (f: (a -> a -> Tot int)) (x y: a) : Tot bool | let my_bool_of_compare (#a: Type) (f: a -> a -> Tot int) (x: a) (y: a) : Tot bool
= f x y < 0 | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 11,
"end_line": 1237,
"start_col": 0,
"start_line": 1236
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am
/// Iterates over all terms in [l2] to prepare them for unification with SMT rewriting
let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2
/// Recursively calls equivalent_lists_once.
/// Stops when we're done with unification, or when we didn't make any progress
/// If we didn't make any progress, we have too many candidates for some terms.
/// Accumulates rewritings of l1 and l2 in l1_del and l2_del, with the invariant
/// that the two lists are unifiable at any point
/// The boolean indicates if there is a leftover empty frame
let rec equivalent_lists' (n:nat) (use_smt:bool) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, []
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Try to make progress by matching non-uvars of l2 with candidates in l1
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Should always be smaller or equal to n
// If it is equal, no progress was made.
if use_smt then
// SMT fallback is allowed
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
/// Checks if term for atom t unifies with fall uvars in l
let rec unifies_with_all_uvars (t:term) (l:list atom) (am:amap term) : Tac bool =
match l with
| [] -> true
| hd::tl ->
if unifies_with_all_uvars t tl am then (
// Unified with tail, try this term
let hd_t = select hd am in
if is_uvar hd_t then (
// The head term is a uvar, try unifying
try if unify t hd_t then raise Success else raise Failed
with | Success -> true | _ -> false
) else true // The uvar is not a head term, we do not need to try it
) else false
/// Puts all terms in l1 that cannot unify with the uvars in l2 at the top:
/// They need to be solved first
let rec most_restricted_at_top (l1 l2:list atom) (am:amap term) : Tac (list atom) =
match l1 with
| [] -> []
| hd::tl ->
if unifies_with_all_uvars (select hd am) l2 am then (most_restricted_at_top tl l2 am) `List.Tot.append` [hd]
else hd::(most_restricted_at_top tl l2 am)
/// Core AC-unification tactic.
/// First remove all trivially equal terms, then try to decide equivalence.
/// Assumes that l1 does not contain any vprop uvar.
/// If it succeeds, returns permutations of l1, l2, and a boolean indicating
/// if l2 has a trailing empty frame to be unified
let equivalent_lists (use_smt:bool) (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term)
= let l1, l2, l1_del, l2_del = trivial_cancels l1 l2 am in
let l1 = most_restricted_at_top l1 l2 am in
let n = List.Tot.length l1 in
let l1_del, l2_del, emp_frame, uvar_terms = equivalent_lists' n use_smt l1 l2 l1_del l2_del am in
l1_del, l2_del, emp_frame, uvar_terms
(* Helpers to relate the actual terms to their representation as a list of atoms *)
open FStar.Reflection.V2.Derived.Lemmas
let rec list_to_string (l:list term) : Tac string =
match l with
| [] -> "end"
| hd::tl -> term_to_string hd ^ " " ^ list_to_string tl
let rec mdenote_gen (#a:Type u#aa) (unit:a) (mult:a -> a -> a) (am:amap a) (e:exp) : a =
match e with
| Unit -> unit
| Atom x -> select x am
| Mult e1 e2 -> mult (mdenote_gen unit mult am e1) (mdenote_gen unit mult am e2)
let rec xsdenote_gen (#a:Type) (unit:a) (mult:a -> a -> a) (am:amap a) (xs:list atom) : a =
match xs with
| [] -> unit
| [x] -> select x am
| x::xs' -> mult (select x am) (xsdenote_gen unit mult am xs')
unfold
let mdenote (#a:Type u#aa) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp) : a =
let open FStar.Algebra.CommMonoid.Equiv in
mdenote_gen (CM?.unit m) (CM?.mult m) am e
unfold
let xsdenote (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom) : a =
let open FStar.Algebra.CommMonoid.Equiv in
xsdenote_gen (CM?.unit m) (CM?.mult m) am xs
// We reimplement List.Tot.Base.append because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_append (#t: Type) (l1 l2: list t) : Pure (list t)
(requires True)
(ensures (fun res -> res == l1 `List.Tot.append` l2))
(decreases l1)
= match l1 with
| [] -> l2
| a :: q -> a :: my_append q l2
let rec flatten (e:exp) : list atom =
match e with
| Unit -> []
| Atom x -> [x]
| Mult e1 e2 -> flatten e1 `my_append` flatten e2
let rec flatten_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs1 xs2:list atom)
: Lemma (xsdenote eq m am (xs1 `my_append` xs2) `CE.EQ?.eq eq` CE.CM?.mult m (xsdenote eq m am xs1)
(xsdenote eq m am xs2)) =
let open FStar.Algebra.CommMonoid.Equiv in
match xs1 with
| [] ->
CM?.identity m (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.unit m) (xsdenote eq m am xs2)) (xsdenote eq m am xs2)
| [x] -> (
if (Nil? xs2)
then (right_identity eq m (select x am);
EQ?.symmetry eq (CM?.mult m (select x am) (CM?.unit m)) (select x am))
else EQ?.reflexivity eq (CM?.mult m (xsdenote eq m am [x]) (xsdenote eq m am xs2)))
| x::xs1' ->
flatten_correct_aux eq m am xs1' xs2;
EQ?.reflexivity eq (select x am);
CM?.congruence m (select x am) (xsdenote eq m am (xs1' `my_append` xs2))
(select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2));
CM?.associativity m (select x am) (xsdenote eq m am xs1') (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)));
EQ?.transitivity eq (CM?.mult m (select x am) (xsdenote eq m am (xs1' `my_append` xs2)))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)))
(CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
let rec flatten_correct (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp)
: Lemma (mdenote eq m am e `CE.EQ?.eq eq` xsdenote eq m am (flatten e)) =
let open FStar.Algebra.CommMonoid.Equiv in
match e with
| Unit -> EQ?.reflexivity eq (CM?.unit m)
| Atom x -> EQ?.reflexivity eq (select x am)
| Mult e1 e2 ->
flatten_correct_aux eq m am (flatten e1) (flatten e2);
EQ?.symmetry eq (xsdenote eq m am (flatten e1 `my_append` flatten e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)));
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CM?.congruence m (mdenote eq m am e1) (mdenote eq m am e2)
(xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2));
EQ?.transitivity eq (CM?.mult m (mdenote eq m am e1) (mdenote eq m am e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)))
(xsdenote eq m am (flatten e1 `my_append` flatten e2))
let monoid_reflect (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e1 e2:exp)
(_ : squash (xsdenote eq m am (flatten e1) `CE.EQ?.eq eq` xsdenote eq m am (flatten e2)))
: squash (mdenote eq m am e1 `CE.EQ?.eq eq` mdenote eq m am e2) =
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CE.EQ?.symmetry eq (mdenote eq m am e2) (xsdenote eq m am (flatten e2));
CE.EQ?.transitivity eq
(xsdenote eq m am (flatten e1))
(xsdenote eq m am (flatten e2))
(mdenote eq m am e2);
CE.EQ?.transitivity eq
(mdenote eq m am e1)
(xsdenote eq m am (flatten e1))
(mdenote eq m am e2)
// Here we sort the variable numbers
// We reimplement List.Tot.Base.sortWith because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_partition (#a: Type) (f: (a -> Tot bool)) (l: list a)
: Pure (list a & list a)
(requires True)
(ensures (fun res -> res == List.Tot.partition f l))
= match l with
| [] -> [], []
| hd::tl ->
let l1, l2 = my_partition f tl in
if f hd
then hd::l1, l2
else l1, hd::l2
let rec partition_ext (#a: Type) (f1 f2: (a -> Tot bool)) (l: list a)
: Lemma
(requires (forall x . f1 x == f2 x))
(ensures (List.Tot.partition f1 l == List.Tot.partition f2 l))
= match l with
| [] -> ()
| hd::tl -> partition_ext f1 f2 tl | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Reflection.V2.Derived.Lemmas",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | f: (_: a -> _: a -> Prims.int) -> x: a -> y: a -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Prims.int",
"Prims.op_LessThan",
"Prims.bool"
] | [] | false | false | false | true | false | let my_bool_of_compare (#a: Type) (f: (a -> a -> Tot int)) (x y: a) : Tot bool =
| f x y < 0 | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.permute | val permute : Type0 | let permute = list atom -> list atom | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 36,
"end_line": 1266,
"start_col": 0,
"start_line": 1266
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am
/// Iterates over all terms in [l2] to prepare them for unification with SMT rewriting
let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2
/// Recursively calls equivalent_lists_once.
/// Stops when we're done with unification, or when we didn't make any progress
/// If we didn't make any progress, we have too many candidates for some terms.
/// Accumulates rewritings of l1 and l2 in l1_del and l2_del, with the invariant
/// that the two lists are unifiable at any point
/// The boolean indicates if there is a leftover empty frame
let rec equivalent_lists' (n:nat) (use_smt:bool) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, []
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Try to make progress by matching non-uvars of l2 with candidates in l1
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Should always be smaller or equal to n
// If it is equal, no progress was made.
if use_smt then
// SMT fallback is allowed
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
/// Checks if term for atom t unifies with fall uvars in l
let rec unifies_with_all_uvars (t:term) (l:list atom) (am:amap term) : Tac bool =
match l with
| [] -> true
| hd::tl ->
if unifies_with_all_uvars t tl am then (
// Unified with tail, try this term
let hd_t = select hd am in
if is_uvar hd_t then (
// The head term is a uvar, try unifying
try if unify t hd_t then raise Success else raise Failed
with | Success -> true | _ -> false
) else true // The uvar is not a head term, we do not need to try it
) else false
/// Puts all terms in l1 that cannot unify with the uvars in l2 at the top:
/// They need to be solved first
let rec most_restricted_at_top (l1 l2:list atom) (am:amap term) : Tac (list atom) =
match l1 with
| [] -> []
| hd::tl ->
if unifies_with_all_uvars (select hd am) l2 am then (most_restricted_at_top tl l2 am) `List.Tot.append` [hd]
else hd::(most_restricted_at_top tl l2 am)
/// Core AC-unification tactic.
/// First remove all trivially equal terms, then try to decide equivalence.
/// Assumes that l1 does not contain any vprop uvar.
/// If it succeeds, returns permutations of l1, l2, and a boolean indicating
/// if l2 has a trailing empty frame to be unified
let equivalent_lists (use_smt:bool) (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term)
= let l1, l2, l1_del, l2_del = trivial_cancels l1 l2 am in
let l1 = most_restricted_at_top l1 l2 am in
let n = List.Tot.length l1 in
let l1_del, l2_del, emp_frame, uvar_terms = equivalent_lists' n use_smt l1 l2 l1_del l2_del am in
l1_del, l2_del, emp_frame, uvar_terms
(* Helpers to relate the actual terms to their representation as a list of atoms *)
open FStar.Reflection.V2.Derived.Lemmas
let rec list_to_string (l:list term) : Tac string =
match l with
| [] -> "end"
| hd::tl -> term_to_string hd ^ " " ^ list_to_string tl
let rec mdenote_gen (#a:Type u#aa) (unit:a) (mult:a -> a -> a) (am:amap a) (e:exp) : a =
match e with
| Unit -> unit
| Atom x -> select x am
| Mult e1 e2 -> mult (mdenote_gen unit mult am e1) (mdenote_gen unit mult am e2)
let rec xsdenote_gen (#a:Type) (unit:a) (mult:a -> a -> a) (am:amap a) (xs:list atom) : a =
match xs with
| [] -> unit
| [x] -> select x am
| x::xs' -> mult (select x am) (xsdenote_gen unit mult am xs')
unfold
let mdenote (#a:Type u#aa) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp) : a =
let open FStar.Algebra.CommMonoid.Equiv in
mdenote_gen (CM?.unit m) (CM?.mult m) am e
unfold
let xsdenote (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom) : a =
let open FStar.Algebra.CommMonoid.Equiv in
xsdenote_gen (CM?.unit m) (CM?.mult m) am xs
// We reimplement List.Tot.Base.append because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_append (#t: Type) (l1 l2: list t) : Pure (list t)
(requires True)
(ensures (fun res -> res == l1 `List.Tot.append` l2))
(decreases l1)
= match l1 with
| [] -> l2
| a :: q -> a :: my_append q l2
let rec flatten (e:exp) : list atom =
match e with
| Unit -> []
| Atom x -> [x]
| Mult e1 e2 -> flatten e1 `my_append` flatten e2
let rec flatten_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs1 xs2:list atom)
: Lemma (xsdenote eq m am (xs1 `my_append` xs2) `CE.EQ?.eq eq` CE.CM?.mult m (xsdenote eq m am xs1)
(xsdenote eq m am xs2)) =
let open FStar.Algebra.CommMonoid.Equiv in
match xs1 with
| [] ->
CM?.identity m (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.unit m) (xsdenote eq m am xs2)) (xsdenote eq m am xs2)
| [x] -> (
if (Nil? xs2)
then (right_identity eq m (select x am);
EQ?.symmetry eq (CM?.mult m (select x am) (CM?.unit m)) (select x am))
else EQ?.reflexivity eq (CM?.mult m (xsdenote eq m am [x]) (xsdenote eq m am xs2)))
| x::xs1' ->
flatten_correct_aux eq m am xs1' xs2;
EQ?.reflexivity eq (select x am);
CM?.congruence m (select x am) (xsdenote eq m am (xs1' `my_append` xs2))
(select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2));
CM?.associativity m (select x am) (xsdenote eq m am xs1') (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)));
EQ?.transitivity eq (CM?.mult m (select x am) (xsdenote eq m am (xs1' `my_append` xs2)))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)))
(CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
let rec flatten_correct (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp)
: Lemma (mdenote eq m am e `CE.EQ?.eq eq` xsdenote eq m am (flatten e)) =
let open FStar.Algebra.CommMonoid.Equiv in
match e with
| Unit -> EQ?.reflexivity eq (CM?.unit m)
| Atom x -> EQ?.reflexivity eq (select x am)
| Mult e1 e2 ->
flatten_correct_aux eq m am (flatten e1) (flatten e2);
EQ?.symmetry eq (xsdenote eq m am (flatten e1 `my_append` flatten e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)));
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CM?.congruence m (mdenote eq m am e1) (mdenote eq m am e2)
(xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2));
EQ?.transitivity eq (CM?.mult m (mdenote eq m am e1) (mdenote eq m am e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)))
(xsdenote eq m am (flatten e1 `my_append` flatten e2))
let monoid_reflect (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e1 e2:exp)
(_ : squash (xsdenote eq m am (flatten e1) `CE.EQ?.eq eq` xsdenote eq m am (flatten e2)))
: squash (mdenote eq m am e1 `CE.EQ?.eq eq` mdenote eq m am e2) =
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CE.EQ?.symmetry eq (mdenote eq m am e2) (xsdenote eq m am (flatten e2));
CE.EQ?.transitivity eq
(xsdenote eq m am (flatten e1))
(xsdenote eq m am (flatten e2))
(mdenote eq m am e2);
CE.EQ?.transitivity eq
(mdenote eq m am e1)
(xsdenote eq m am (flatten e1))
(mdenote eq m am e2)
// Here we sort the variable numbers
// We reimplement List.Tot.Base.sortWith because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_partition (#a: Type) (f: (a -> Tot bool)) (l: list a)
: Pure (list a & list a)
(requires True)
(ensures (fun res -> res == List.Tot.partition f l))
= match l with
| [] -> [], []
| hd::tl ->
let l1, l2 = my_partition f tl in
if f hd
then hd::l1, l2
else l1, hd::l2
let rec partition_ext (#a: Type) (f1 f2: (a -> Tot bool)) (l: list a)
: Lemma
(requires (forall x . f1 x == f2 x))
(ensures (List.Tot.partition f1 l == List.Tot.partition f2 l))
= match l with
| [] -> ()
| hd::tl -> partition_ext f1 f2 tl
let my_bool_of_compare (#a: Type) (f: a -> a -> Tot int) (x: a) (y: a) : Tot bool
= f x y < 0
let rec my_sortWith (#a: Type) (f: (a -> a -> Tot int)) (l:list a)
: Pure (list a)
(requires True)
(ensures (fun res -> res == List.Tot.sortWith f l))
(decreases (List.Tot.length l))
= match l with
| [] -> []
| pivot::tl ->
let hi, lo = my_partition (my_bool_of_compare f pivot) tl in
partition_ext (my_bool_of_compare f pivot) (List.Tot.bool_of_compare f pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f pivot) tl;
my_append (my_sortWith f lo) (pivot::my_sortWith f hi)
let rec sortWith_ext (#a: Type) (f1 f2: (a -> a -> Tot int)) (l: list a)
: Lemma
(requires (forall x y . f1 x y == f2 x y))
(ensures (List.Tot.sortWith f1 l == List.Tot.sortWith f2 l))
(decreases (List.Tot.length l))
= match l with
| [] -> ()
| pivot::tl ->
partition_ext (List.Tot.bool_of_compare f1 pivot) (List.Tot.bool_of_compare f2 pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f1 pivot) tl;
let hi, lo = List.Tot.partition (List.Tot.bool_of_compare f1 pivot) tl in
sortWith_ext f1 f2 lo;
sortWith_ext f1 f2 hi | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Reflection.V2.Derived.Lemmas",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Type0 | Prims.Tot | [
"total"
] | [] | [
"Prims.list",
"Steel.Effect.Common.atom"
] | [] | false | false | false | true | true | let permute =
| list atom -> list atom | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.my_compare_of_bool | val my_compare_of_bool (#a: eqtype) (rel: (a -> a -> Tot bool)) (x y: a) : Tot int | val my_compare_of_bool (#a: eqtype) (rel: (a -> a -> Tot bool)) (x y: a) : Tot int | let my_compare_of_bool (#a:eqtype) (rel: a -> a -> Tot bool) (x: a) (y: a) : Tot int
= if x `rel` y then -1
else if x = y then 0
else 1 | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 10,
"end_line": 1271,
"start_col": 0,
"start_line": 1268
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am
/// Iterates over all terms in [l2] to prepare them for unification with SMT rewriting
let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2
/// Recursively calls equivalent_lists_once.
/// Stops when we're done with unification, or when we didn't make any progress
/// If we didn't make any progress, we have too many candidates for some terms.
/// Accumulates rewritings of l1 and l2 in l1_del and l2_del, with the invariant
/// that the two lists are unifiable at any point
/// The boolean indicates if there is a leftover empty frame
let rec equivalent_lists' (n:nat) (use_smt:bool) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, []
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Try to make progress by matching non-uvars of l2 with candidates in l1
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Should always be smaller or equal to n
// If it is equal, no progress was made.
if use_smt then
// SMT fallback is allowed
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
/// Checks if term for atom t unifies with fall uvars in l
let rec unifies_with_all_uvars (t:term) (l:list atom) (am:amap term) : Tac bool =
match l with
| [] -> true
| hd::tl ->
if unifies_with_all_uvars t tl am then (
// Unified with tail, try this term
let hd_t = select hd am in
if is_uvar hd_t then (
// The head term is a uvar, try unifying
try if unify t hd_t then raise Success else raise Failed
with | Success -> true | _ -> false
) else true // The uvar is not a head term, we do not need to try it
) else false
/// Puts all terms in l1 that cannot unify with the uvars in l2 at the top:
/// They need to be solved first
let rec most_restricted_at_top (l1 l2:list atom) (am:amap term) : Tac (list atom) =
match l1 with
| [] -> []
| hd::tl ->
if unifies_with_all_uvars (select hd am) l2 am then (most_restricted_at_top tl l2 am) `List.Tot.append` [hd]
else hd::(most_restricted_at_top tl l2 am)
/// Core AC-unification tactic.
/// First remove all trivially equal terms, then try to decide equivalence.
/// Assumes that l1 does not contain any vprop uvar.
/// If it succeeds, returns permutations of l1, l2, and a boolean indicating
/// if l2 has a trailing empty frame to be unified
let equivalent_lists (use_smt:bool) (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term)
= let l1, l2, l1_del, l2_del = trivial_cancels l1 l2 am in
let l1 = most_restricted_at_top l1 l2 am in
let n = List.Tot.length l1 in
let l1_del, l2_del, emp_frame, uvar_terms = equivalent_lists' n use_smt l1 l2 l1_del l2_del am in
l1_del, l2_del, emp_frame, uvar_terms
(* Helpers to relate the actual terms to their representation as a list of atoms *)
open FStar.Reflection.V2.Derived.Lemmas
let rec list_to_string (l:list term) : Tac string =
match l with
| [] -> "end"
| hd::tl -> term_to_string hd ^ " " ^ list_to_string tl
let rec mdenote_gen (#a:Type u#aa) (unit:a) (mult:a -> a -> a) (am:amap a) (e:exp) : a =
match e with
| Unit -> unit
| Atom x -> select x am
| Mult e1 e2 -> mult (mdenote_gen unit mult am e1) (mdenote_gen unit mult am e2)
let rec xsdenote_gen (#a:Type) (unit:a) (mult:a -> a -> a) (am:amap a) (xs:list atom) : a =
match xs with
| [] -> unit
| [x] -> select x am
| x::xs' -> mult (select x am) (xsdenote_gen unit mult am xs')
unfold
let mdenote (#a:Type u#aa) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp) : a =
let open FStar.Algebra.CommMonoid.Equiv in
mdenote_gen (CM?.unit m) (CM?.mult m) am e
unfold
let xsdenote (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom) : a =
let open FStar.Algebra.CommMonoid.Equiv in
xsdenote_gen (CM?.unit m) (CM?.mult m) am xs
// We reimplement List.Tot.Base.append because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_append (#t: Type) (l1 l2: list t) : Pure (list t)
(requires True)
(ensures (fun res -> res == l1 `List.Tot.append` l2))
(decreases l1)
= match l1 with
| [] -> l2
| a :: q -> a :: my_append q l2
let rec flatten (e:exp) : list atom =
match e with
| Unit -> []
| Atom x -> [x]
| Mult e1 e2 -> flatten e1 `my_append` flatten e2
let rec flatten_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs1 xs2:list atom)
: Lemma (xsdenote eq m am (xs1 `my_append` xs2) `CE.EQ?.eq eq` CE.CM?.mult m (xsdenote eq m am xs1)
(xsdenote eq m am xs2)) =
let open FStar.Algebra.CommMonoid.Equiv in
match xs1 with
| [] ->
CM?.identity m (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.unit m) (xsdenote eq m am xs2)) (xsdenote eq m am xs2)
| [x] -> (
if (Nil? xs2)
then (right_identity eq m (select x am);
EQ?.symmetry eq (CM?.mult m (select x am) (CM?.unit m)) (select x am))
else EQ?.reflexivity eq (CM?.mult m (xsdenote eq m am [x]) (xsdenote eq m am xs2)))
| x::xs1' ->
flatten_correct_aux eq m am xs1' xs2;
EQ?.reflexivity eq (select x am);
CM?.congruence m (select x am) (xsdenote eq m am (xs1' `my_append` xs2))
(select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2));
CM?.associativity m (select x am) (xsdenote eq m am xs1') (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)));
EQ?.transitivity eq (CM?.mult m (select x am) (xsdenote eq m am (xs1' `my_append` xs2)))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)))
(CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
let rec flatten_correct (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp)
: Lemma (mdenote eq m am e `CE.EQ?.eq eq` xsdenote eq m am (flatten e)) =
let open FStar.Algebra.CommMonoid.Equiv in
match e with
| Unit -> EQ?.reflexivity eq (CM?.unit m)
| Atom x -> EQ?.reflexivity eq (select x am)
| Mult e1 e2 ->
flatten_correct_aux eq m am (flatten e1) (flatten e2);
EQ?.symmetry eq (xsdenote eq m am (flatten e1 `my_append` flatten e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)));
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CM?.congruence m (mdenote eq m am e1) (mdenote eq m am e2)
(xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2));
EQ?.transitivity eq (CM?.mult m (mdenote eq m am e1) (mdenote eq m am e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)))
(xsdenote eq m am (flatten e1 `my_append` flatten e2))
let monoid_reflect (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e1 e2:exp)
(_ : squash (xsdenote eq m am (flatten e1) `CE.EQ?.eq eq` xsdenote eq m am (flatten e2)))
: squash (mdenote eq m am e1 `CE.EQ?.eq eq` mdenote eq m am e2) =
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CE.EQ?.symmetry eq (mdenote eq m am e2) (xsdenote eq m am (flatten e2));
CE.EQ?.transitivity eq
(xsdenote eq m am (flatten e1))
(xsdenote eq m am (flatten e2))
(mdenote eq m am e2);
CE.EQ?.transitivity eq
(mdenote eq m am e1)
(xsdenote eq m am (flatten e1))
(mdenote eq m am e2)
// Here we sort the variable numbers
// We reimplement List.Tot.Base.sortWith because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_partition (#a: Type) (f: (a -> Tot bool)) (l: list a)
: Pure (list a & list a)
(requires True)
(ensures (fun res -> res == List.Tot.partition f l))
= match l with
| [] -> [], []
| hd::tl ->
let l1, l2 = my_partition f tl in
if f hd
then hd::l1, l2
else l1, hd::l2
let rec partition_ext (#a: Type) (f1 f2: (a -> Tot bool)) (l: list a)
: Lemma
(requires (forall x . f1 x == f2 x))
(ensures (List.Tot.partition f1 l == List.Tot.partition f2 l))
= match l with
| [] -> ()
| hd::tl -> partition_ext f1 f2 tl
let my_bool_of_compare (#a: Type) (f: a -> a -> Tot int) (x: a) (y: a) : Tot bool
= f x y < 0
let rec my_sortWith (#a: Type) (f: (a -> a -> Tot int)) (l:list a)
: Pure (list a)
(requires True)
(ensures (fun res -> res == List.Tot.sortWith f l))
(decreases (List.Tot.length l))
= match l with
| [] -> []
| pivot::tl ->
let hi, lo = my_partition (my_bool_of_compare f pivot) tl in
partition_ext (my_bool_of_compare f pivot) (List.Tot.bool_of_compare f pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f pivot) tl;
my_append (my_sortWith f lo) (pivot::my_sortWith f hi)
let rec sortWith_ext (#a: Type) (f1 f2: (a -> a -> Tot int)) (l: list a)
: Lemma
(requires (forall x y . f1 x y == f2 x y))
(ensures (List.Tot.sortWith f1 l == List.Tot.sortWith f2 l))
(decreases (List.Tot.length l))
= match l with
| [] -> ()
| pivot::tl ->
partition_ext (List.Tot.bool_of_compare f1 pivot) (List.Tot.bool_of_compare f2 pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f1 pivot) tl;
let hi, lo = List.Tot.partition (List.Tot.bool_of_compare f1 pivot) tl in
sortWith_ext f1 f2 lo;
sortWith_ext f1 f2 hi
let permute = list atom -> list atom | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Reflection.V2.Derived.Lemmas",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | rel: (_: a -> _: a -> Prims.bool) -> x: a -> y: a -> Prims.int | Prims.Tot | [
"total"
] | [] | [
"Prims.eqtype",
"Prims.bool",
"Prims.op_Minus",
"Prims.op_Equality",
"Prims.int"
] | [] | false | false | false | false | false | let my_compare_of_bool (#a: eqtype) (rel: (a -> a -> Tot bool)) (x y: a) : Tot int =
| if x `rel` y then - 1 else if x = y then 0 else 1 | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.trivial_cancels | val trivial_cancels (l1 l2: list atom) (am: amap term)
: Tac (list atom * list atom * list atom * list atom) | val trivial_cancels (l1 l2: list atom) (am: amap term)
: Tac (list atom * list atom * list atom * list atom) | let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 76,
"end_line": 828,
"start_col": 0,
"start_line": 820
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
l1: Prims.list Steel.Effect.Common.atom ->
l2: Prims.list Steel.Effect.Common.atom ->
am: Steel.Effect.Common.amap FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac
(((Prims.list Steel.Effect.Common.atom * Prims.list Steel.Effect.Common.atom) *
Prims.list Steel.Effect.Common.atom) *
Prims.list Steel.Effect.Common.atom) | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.list",
"Steel.Effect.Common.atom",
"Steel.Effect.Common.amap",
"FStar.Tactics.NamedView.term",
"FStar.Pervasives.Native.Mktuple4",
"Prims.Nil",
"FStar.Pervasives.Native.tuple4",
"Prims.bool",
"Prims.Cons",
"Steel.Effect.Common.trivial_cancels",
"FStar.Pervasives.Native.tuple2",
"Steel.Effect.Common.trivial_cancel"
] | [
"recursion"
] | false | true | false | false | false | let rec trivial_cancels (l1 l2: list atom) (am: amap term)
: Tac (list atom * list atom * list atom * list atom) =
| match l1 with
| [] -> [], l2, [], []
| hd :: tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd :: l1'),
l2',
(if b then hd :: l1_del else l1_del),
(if b then hd :: l2_del else l2_del) | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.visit_br | val visit_br (ff: (term -> Tac unit)) (b: branch) : Tac unit | val visit_br (ff: (term -> Tac unit)) (b: branch) : Tac unit | let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 30,
"end_line": 719,
"start_col": 0,
"start_line": 655
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
ff: (_: FStar.Tactics.NamedView.term -> FStar.Tactics.Effect.Tac Prims.unit) ->
b: FStar.Tactics.NamedView.branch
-> FStar.Tactics.Effect.Tac Prims.unit | FStar.Tactics.Effect.Tac | [] | [
"visit_tm",
"visit_br",
"visit_comp"
] | [
"FStar.Tactics.NamedView.term",
"Prims.unit",
"FStar.Tactics.NamedView.branch",
"FStar.Tactics.NamedView.pattern",
"Steel.Effect.Common.visit_tm"
] | [
"mutual recursion"
] | false | true | false | false | false | let rec visit_br (ff: (term -> Tac unit)) (b: branch) : Tac unit =
| let p, t = b in
visit_tm ff t | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.visit_comp | val visit_comp (ff: (term -> Tac unit)) (c: comp) : Tac unit | val visit_comp (ff: (term -> Tac unit)) (c: comp) : Tac unit | let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 30,
"end_line": 719,
"start_col": 0,
"start_line": 655
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
ff: (_: FStar.Tactics.NamedView.term -> FStar.Tactics.Effect.Tac Prims.unit) ->
c: FStar.Tactics.NamedView.comp
-> FStar.Tactics.Effect.Tac Prims.unit | FStar.Tactics.Effect.Tac | [] | [
"visit_tm",
"visit_br",
"visit_comp"
] | [
"FStar.Tactics.NamedView.term",
"Prims.unit",
"FStar.Tactics.NamedView.comp",
"FStar.Stubs.Reflection.Types.typ",
"Steel.Effect.Common.visit_tm",
"FStar.Stubs.Reflection.Types.term",
"FStar.Stubs.Reflection.V2.Data.universes",
"FStar.Stubs.Reflection.Types.name",
"Prims.list",
"FStar.Stubs.Reflection.V2.Data.argv",
"FStar.Tactics.Util.iter",
"FStar.Pervasives.Native.tuple2",
"FStar.Stubs.Reflection.V2.Data.aqualv",
"FStar.Tactics.NamedView.inspect_comp"
] | [
"mutual recursion"
] | false | true | false | false | false | let rec visit_comp (ff: (term -> Tac unit)) (c: comp) : Tac unit =
| let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.remove_from_list | val remove_from_list (t: atom) (l: list atom) : Tac (list atom) | val remove_from_list (t: atom) (l: list atom) : Tac (list atom) | let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 62,
"end_line": 861,
"start_col": 0,
"start_line": 858
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | t: Steel.Effect.Common.atom -> l: Prims.list Steel.Effect.Common.atom
-> FStar.Tactics.Effect.Tac (Prims.list Steel.Effect.Common.atom) | FStar.Tactics.Effect.Tac | [] | [] | [
"Steel.Effect.Common.atom",
"Prims.list",
"Prims.Nil",
"Prims.unit",
"FStar.Tactics.V2.Derived.fail",
"Prims.op_Equality",
"Prims.bool",
"Prims.Cons",
"Steel.Effect.Common.remove_from_list"
] | [
"recursion"
] | false | true | false | false | false | let rec remove_from_list (t: atom) (l: list atom) : Tac (list atom) =
| match l with
| [] ->
fail "atom in remove_from_list not found: should not happen";
[]
| hd :: tl -> if t = hd then tl else hd :: remove_from_list t tl | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.sort | val sort:permute | val sort:permute | let sort : permute = my_sortWith #int (my_compare_of_bool (<)) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 62,
"end_line": 1273,
"start_col": 0,
"start_line": 1273
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am
/// Iterates over all terms in [l2] to prepare them for unification with SMT rewriting
let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2
/// Recursively calls equivalent_lists_once.
/// Stops when we're done with unification, or when we didn't make any progress
/// If we didn't make any progress, we have too many candidates for some terms.
/// Accumulates rewritings of l1 and l2 in l1_del and l2_del, with the invariant
/// that the two lists are unifiable at any point
/// The boolean indicates if there is a leftover empty frame
let rec equivalent_lists' (n:nat) (use_smt:bool) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, []
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Try to make progress by matching non-uvars of l2 with candidates in l1
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Should always be smaller or equal to n
// If it is equal, no progress was made.
if use_smt then
// SMT fallback is allowed
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
/// Checks if term for atom t unifies with fall uvars in l
let rec unifies_with_all_uvars (t:term) (l:list atom) (am:amap term) : Tac bool =
match l with
| [] -> true
| hd::tl ->
if unifies_with_all_uvars t tl am then (
// Unified with tail, try this term
let hd_t = select hd am in
if is_uvar hd_t then (
// The head term is a uvar, try unifying
try if unify t hd_t then raise Success else raise Failed
with | Success -> true | _ -> false
) else true // The uvar is not a head term, we do not need to try it
) else false
/// Puts all terms in l1 that cannot unify with the uvars in l2 at the top:
/// They need to be solved first
let rec most_restricted_at_top (l1 l2:list atom) (am:amap term) : Tac (list atom) =
match l1 with
| [] -> []
| hd::tl ->
if unifies_with_all_uvars (select hd am) l2 am then (most_restricted_at_top tl l2 am) `List.Tot.append` [hd]
else hd::(most_restricted_at_top tl l2 am)
/// Core AC-unification tactic.
/// First remove all trivially equal terms, then try to decide equivalence.
/// Assumes that l1 does not contain any vprop uvar.
/// If it succeeds, returns permutations of l1, l2, and a boolean indicating
/// if l2 has a trailing empty frame to be unified
let equivalent_lists (use_smt:bool) (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term)
= let l1, l2, l1_del, l2_del = trivial_cancels l1 l2 am in
let l1 = most_restricted_at_top l1 l2 am in
let n = List.Tot.length l1 in
let l1_del, l2_del, emp_frame, uvar_terms = equivalent_lists' n use_smt l1 l2 l1_del l2_del am in
l1_del, l2_del, emp_frame, uvar_terms
(* Helpers to relate the actual terms to their representation as a list of atoms *)
open FStar.Reflection.V2.Derived.Lemmas
let rec list_to_string (l:list term) : Tac string =
match l with
| [] -> "end"
| hd::tl -> term_to_string hd ^ " " ^ list_to_string tl
let rec mdenote_gen (#a:Type u#aa) (unit:a) (mult:a -> a -> a) (am:amap a) (e:exp) : a =
match e with
| Unit -> unit
| Atom x -> select x am
| Mult e1 e2 -> mult (mdenote_gen unit mult am e1) (mdenote_gen unit mult am e2)
let rec xsdenote_gen (#a:Type) (unit:a) (mult:a -> a -> a) (am:amap a) (xs:list atom) : a =
match xs with
| [] -> unit
| [x] -> select x am
| x::xs' -> mult (select x am) (xsdenote_gen unit mult am xs')
unfold
let mdenote (#a:Type u#aa) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp) : a =
let open FStar.Algebra.CommMonoid.Equiv in
mdenote_gen (CM?.unit m) (CM?.mult m) am e
unfold
let xsdenote (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom) : a =
let open FStar.Algebra.CommMonoid.Equiv in
xsdenote_gen (CM?.unit m) (CM?.mult m) am xs
// We reimplement List.Tot.Base.append because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_append (#t: Type) (l1 l2: list t) : Pure (list t)
(requires True)
(ensures (fun res -> res == l1 `List.Tot.append` l2))
(decreases l1)
= match l1 with
| [] -> l2
| a :: q -> a :: my_append q l2
let rec flatten (e:exp) : list atom =
match e with
| Unit -> []
| Atom x -> [x]
| Mult e1 e2 -> flatten e1 `my_append` flatten e2
let rec flatten_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs1 xs2:list atom)
: Lemma (xsdenote eq m am (xs1 `my_append` xs2) `CE.EQ?.eq eq` CE.CM?.mult m (xsdenote eq m am xs1)
(xsdenote eq m am xs2)) =
let open FStar.Algebra.CommMonoid.Equiv in
match xs1 with
| [] ->
CM?.identity m (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.unit m) (xsdenote eq m am xs2)) (xsdenote eq m am xs2)
| [x] -> (
if (Nil? xs2)
then (right_identity eq m (select x am);
EQ?.symmetry eq (CM?.mult m (select x am) (CM?.unit m)) (select x am))
else EQ?.reflexivity eq (CM?.mult m (xsdenote eq m am [x]) (xsdenote eq m am xs2)))
| x::xs1' ->
flatten_correct_aux eq m am xs1' xs2;
EQ?.reflexivity eq (select x am);
CM?.congruence m (select x am) (xsdenote eq m am (xs1' `my_append` xs2))
(select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2));
CM?.associativity m (select x am) (xsdenote eq m am xs1') (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)));
EQ?.transitivity eq (CM?.mult m (select x am) (xsdenote eq m am (xs1' `my_append` xs2)))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)))
(CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
let rec flatten_correct (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp)
: Lemma (mdenote eq m am e `CE.EQ?.eq eq` xsdenote eq m am (flatten e)) =
let open FStar.Algebra.CommMonoid.Equiv in
match e with
| Unit -> EQ?.reflexivity eq (CM?.unit m)
| Atom x -> EQ?.reflexivity eq (select x am)
| Mult e1 e2 ->
flatten_correct_aux eq m am (flatten e1) (flatten e2);
EQ?.symmetry eq (xsdenote eq m am (flatten e1 `my_append` flatten e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)));
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CM?.congruence m (mdenote eq m am e1) (mdenote eq m am e2)
(xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2));
EQ?.transitivity eq (CM?.mult m (mdenote eq m am e1) (mdenote eq m am e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)))
(xsdenote eq m am (flatten e1 `my_append` flatten e2))
let monoid_reflect (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e1 e2:exp)
(_ : squash (xsdenote eq m am (flatten e1) `CE.EQ?.eq eq` xsdenote eq m am (flatten e2)))
: squash (mdenote eq m am e1 `CE.EQ?.eq eq` mdenote eq m am e2) =
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CE.EQ?.symmetry eq (mdenote eq m am e2) (xsdenote eq m am (flatten e2));
CE.EQ?.transitivity eq
(xsdenote eq m am (flatten e1))
(xsdenote eq m am (flatten e2))
(mdenote eq m am e2);
CE.EQ?.transitivity eq
(mdenote eq m am e1)
(xsdenote eq m am (flatten e1))
(mdenote eq m am e2)
// Here we sort the variable numbers
// We reimplement List.Tot.Base.sortWith because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_partition (#a: Type) (f: (a -> Tot bool)) (l: list a)
: Pure (list a & list a)
(requires True)
(ensures (fun res -> res == List.Tot.partition f l))
= match l with
| [] -> [], []
| hd::tl ->
let l1, l2 = my_partition f tl in
if f hd
then hd::l1, l2
else l1, hd::l2
let rec partition_ext (#a: Type) (f1 f2: (a -> Tot bool)) (l: list a)
: Lemma
(requires (forall x . f1 x == f2 x))
(ensures (List.Tot.partition f1 l == List.Tot.partition f2 l))
= match l with
| [] -> ()
| hd::tl -> partition_ext f1 f2 tl
let my_bool_of_compare (#a: Type) (f: a -> a -> Tot int) (x: a) (y: a) : Tot bool
= f x y < 0
let rec my_sortWith (#a: Type) (f: (a -> a -> Tot int)) (l:list a)
: Pure (list a)
(requires True)
(ensures (fun res -> res == List.Tot.sortWith f l))
(decreases (List.Tot.length l))
= match l with
| [] -> []
| pivot::tl ->
let hi, lo = my_partition (my_bool_of_compare f pivot) tl in
partition_ext (my_bool_of_compare f pivot) (List.Tot.bool_of_compare f pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f pivot) tl;
my_append (my_sortWith f lo) (pivot::my_sortWith f hi)
let rec sortWith_ext (#a: Type) (f1 f2: (a -> a -> Tot int)) (l: list a)
: Lemma
(requires (forall x y . f1 x y == f2 x y))
(ensures (List.Tot.sortWith f1 l == List.Tot.sortWith f2 l))
(decreases (List.Tot.length l))
= match l with
| [] -> ()
| pivot::tl ->
partition_ext (List.Tot.bool_of_compare f1 pivot) (List.Tot.bool_of_compare f2 pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f1 pivot) tl;
let hi, lo = List.Tot.partition (List.Tot.bool_of_compare f1 pivot) tl in
sortWith_ext f1 f2 lo;
sortWith_ext f1 f2 hi
let permute = list atom -> list atom
let my_compare_of_bool (#a:eqtype) (rel: a -> a -> Tot bool) (x: a) (y: a) : Tot int
= if x `rel` y then -1
else if x = y then 0
else 1 | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Reflection.V2.Derived.Lemmas",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Steel.Effect.Common.permute | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.my_sortWith",
"Prims.int",
"Steel.Effect.Common.my_compare_of_bool",
"Prims.op_LessThan"
] | [] | false | false | false | true | false | let sort:permute =
| my_sortWith #int (my_compare_of_bool ( < )) | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.print_atoms | val print_atoms (l: list atom) (am: amap term) : Tac string | val print_atoms (l: list atom) (am: amap term) : Tac string | let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 71,
"end_line": 838,
"start_col": 0,
"start_line": 834
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list Steel.Effect.Common.atom -> am: Steel.Effect.Common.amap FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac Prims.string | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.list",
"Steel.Effect.Common.atom",
"Steel.Effect.Common.amap",
"FStar.Tactics.NamedView.term",
"Prims.string",
"FStar.Stubs.Tactics.V2.Builtins.term_to_string",
"Steel.Effect.Common.select",
"Prims.op_Hat",
"Steel.Effect.Common.print_atoms"
] | [
"recursion"
] | false | true | false | false | false | let rec print_atoms (l: list atom) (am: amap term) : Tac string =
| match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd :: tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.where | val where : x: FStar.Tactics.NamedView.term -> xs: Prims.list FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac (FStar.Pervasives.Native.option Prims.nat) | let where = where_aux 0 | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 23,
"end_line": 1613,
"start_col": 0,
"start_line": 1613
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am
/// Iterates over all terms in [l2] to prepare them for unification with SMT rewriting
let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2
/// Recursively calls equivalent_lists_once.
/// Stops when we're done with unification, or when we didn't make any progress
/// If we didn't make any progress, we have too many candidates for some terms.
/// Accumulates rewritings of l1 and l2 in l1_del and l2_del, with the invariant
/// that the two lists are unifiable at any point
/// The boolean indicates if there is a leftover empty frame
let rec equivalent_lists' (n:nat) (use_smt:bool) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, []
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Try to make progress by matching non-uvars of l2 with candidates in l1
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Should always be smaller or equal to n
// If it is equal, no progress was made.
if use_smt then
// SMT fallback is allowed
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
/// Checks if term for atom t unifies with fall uvars in l
let rec unifies_with_all_uvars (t:term) (l:list atom) (am:amap term) : Tac bool =
match l with
| [] -> true
| hd::tl ->
if unifies_with_all_uvars t tl am then (
// Unified with tail, try this term
let hd_t = select hd am in
if is_uvar hd_t then (
// The head term is a uvar, try unifying
try if unify t hd_t then raise Success else raise Failed
with | Success -> true | _ -> false
) else true // The uvar is not a head term, we do not need to try it
) else false
/// Puts all terms in l1 that cannot unify with the uvars in l2 at the top:
/// They need to be solved first
let rec most_restricted_at_top (l1 l2:list atom) (am:amap term) : Tac (list atom) =
match l1 with
| [] -> []
| hd::tl ->
if unifies_with_all_uvars (select hd am) l2 am then (most_restricted_at_top tl l2 am) `List.Tot.append` [hd]
else hd::(most_restricted_at_top tl l2 am)
/// Core AC-unification tactic.
/// First remove all trivially equal terms, then try to decide equivalence.
/// Assumes that l1 does not contain any vprop uvar.
/// If it succeeds, returns permutations of l1, l2, and a boolean indicating
/// if l2 has a trailing empty frame to be unified
let equivalent_lists (use_smt:bool) (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term)
= let l1, l2, l1_del, l2_del = trivial_cancels l1 l2 am in
let l1 = most_restricted_at_top l1 l2 am in
let n = List.Tot.length l1 in
let l1_del, l2_del, emp_frame, uvar_terms = equivalent_lists' n use_smt l1 l2 l1_del l2_del am in
l1_del, l2_del, emp_frame, uvar_terms
(* Helpers to relate the actual terms to their representation as a list of atoms *)
open FStar.Reflection.V2.Derived.Lemmas
let rec list_to_string (l:list term) : Tac string =
match l with
| [] -> "end"
| hd::tl -> term_to_string hd ^ " " ^ list_to_string tl
let rec mdenote_gen (#a:Type u#aa) (unit:a) (mult:a -> a -> a) (am:amap a) (e:exp) : a =
match e with
| Unit -> unit
| Atom x -> select x am
| Mult e1 e2 -> mult (mdenote_gen unit mult am e1) (mdenote_gen unit mult am e2)
let rec xsdenote_gen (#a:Type) (unit:a) (mult:a -> a -> a) (am:amap a) (xs:list atom) : a =
match xs with
| [] -> unit
| [x] -> select x am
| x::xs' -> mult (select x am) (xsdenote_gen unit mult am xs')
unfold
let mdenote (#a:Type u#aa) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp) : a =
let open FStar.Algebra.CommMonoid.Equiv in
mdenote_gen (CM?.unit m) (CM?.mult m) am e
unfold
let xsdenote (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom) : a =
let open FStar.Algebra.CommMonoid.Equiv in
xsdenote_gen (CM?.unit m) (CM?.mult m) am xs
// We reimplement List.Tot.Base.append because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_append (#t: Type) (l1 l2: list t) : Pure (list t)
(requires True)
(ensures (fun res -> res == l1 `List.Tot.append` l2))
(decreases l1)
= match l1 with
| [] -> l2
| a :: q -> a :: my_append q l2
let rec flatten (e:exp) : list atom =
match e with
| Unit -> []
| Atom x -> [x]
| Mult e1 e2 -> flatten e1 `my_append` flatten e2
let rec flatten_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs1 xs2:list atom)
: Lemma (xsdenote eq m am (xs1 `my_append` xs2) `CE.EQ?.eq eq` CE.CM?.mult m (xsdenote eq m am xs1)
(xsdenote eq m am xs2)) =
let open FStar.Algebra.CommMonoid.Equiv in
match xs1 with
| [] ->
CM?.identity m (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.unit m) (xsdenote eq m am xs2)) (xsdenote eq m am xs2)
| [x] -> (
if (Nil? xs2)
then (right_identity eq m (select x am);
EQ?.symmetry eq (CM?.mult m (select x am) (CM?.unit m)) (select x am))
else EQ?.reflexivity eq (CM?.mult m (xsdenote eq m am [x]) (xsdenote eq m am xs2)))
| x::xs1' ->
flatten_correct_aux eq m am xs1' xs2;
EQ?.reflexivity eq (select x am);
CM?.congruence m (select x am) (xsdenote eq m am (xs1' `my_append` xs2))
(select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2));
CM?.associativity m (select x am) (xsdenote eq m am xs1') (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)));
EQ?.transitivity eq (CM?.mult m (select x am) (xsdenote eq m am (xs1' `my_append` xs2)))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)))
(CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
let rec flatten_correct (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp)
: Lemma (mdenote eq m am e `CE.EQ?.eq eq` xsdenote eq m am (flatten e)) =
let open FStar.Algebra.CommMonoid.Equiv in
match e with
| Unit -> EQ?.reflexivity eq (CM?.unit m)
| Atom x -> EQ?.reflexivity eq (select x am)
| Mult e1 e2 ->
flatten_correct_aux eq m am (flatten e1) (flatten e2);
EQ?.symmetry eq (xsdenote eq m am (flatten e1 `my_append` flatten e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)));
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CM?.congruence m (mdenote eq m am e1) (mdenote eq m am e2)
(xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2));
EQ?.transitivity eq (CM?.mult m (mdenote eq m am e1) (mdenote eq m am e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)))
(xsdenote eq m am (flatten e1 `my_append` flatten e2))
let monoid_reflect (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e1 e2:exp)
(_ : squash (xsdenote eq m am (flatten e1) `CE.EQ?.eq eq` xsdenote eq m am (flatten e2)))
: squash (mdenote eq m am e1 `CE.EQ?.eq eq` mdenote eq m am e2) =
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CE.EQ?.symmetry eq (mdenote eq m am e2) (xsdenote eq m am (flatten e2));
CE.EQ?.transitivity eq
(xsdenote eq m am (flatten e1))
(xsdenote eq m am (flatten e2))
(mdenote eq m am e2);
CE.EQ?.transitivity eq
(mdenote eq m am e1)
(xsdenote eq m am (flatten e1))
(mdenote eq m am e2)
// Here we sort the variable numbers
// We reimplement List.Tot.Base.sortWith because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_partition (#a: Type) (f: (a -> Tot bool)) (l: list a)
: Pure (list a & list a)
(requires True)
(ensures (fun res -> res == List.Tot.partition f l))
= match l with
| [] -> [], []
| hd::tl ->
let l1, l2 = my_partition f tl in
if f hd
then hd::l1, l2
else l1, hd::l2
let rec partition_ext (#a: Type) (f1 f2: (a -> Tot bool)) (l: list a)
: Lemma
(requires (forall x . f1 x == f2 x))
(ensures (List.Tot.partition f1 l == List.Tot.partition f2 l))
= match l with
| [] -> ()
| hd::tl -> partition_ext f1 f2 tl
let my_bool_of_compare (#a: Type) (f: a -> a -> Tot int) (x: a) (y: a) : Tot bool
= f x y < 0
let rec my_sortWith (#a: Type) (f: (a -> a -> Tot int)) (l:list a)
: Pure (list a)
(requires True)
(ensures (fun res -> res == List.Tot.sortWith f l))
(decreases (List.Tot.length l))
= match l with
| [] -> []
| pivot::tl ->
let hi, lo = my_partition (my_bool_of_compare f pivot) tl in
partition_ext (my_bool_of_compare f pivot) (List.Tot.bool_of_compare f pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f pivot) tl;
my_append (my_sortWith f lo) (pivot::my_sortWith f hi)
let rec sortWith_ext (#a: Type) (f1 f2: (a -> a -> Tot int)) (l: list a)
: Lemma
(requires (forall x y . f1 x y == f2 x y))
(ensures (List.Tot.sortWith f1 l == List.Tot.sortWith f2 l))
(decreases (List.Tot.length l))
= match l with
| [] -> ()
| pivot::tl ->
partition_ext (List.Tot.bool_of_compare f1 pivot) (List.Tot.bool_of_compare f2 pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f1 pivot) tl;
let hi, lo = List.Tot.partition (List.Tot.bool_of_compare f1 pivot) tl in
sortWith_ext f1 f2 lo;
sortWith_ext f1 f2 hi
let permute = list atom -> list atom
let my_compare_of_bool (#a:eqtype) (rel: a -> a -> Tot bool) (x: a) (y: a) : Tot int
= if x `rel` y then -1
else if x = y then 0
else 1
let sort : permute = my_sortWith #int (my_compare_of_bool (<))
#push-options "--fuel 1 --ifuel 1"
let lemma_xsdenote_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (hd:atom) (tl:list atom)
: Lemma (xsdenote eq m am (hd::tl) `CE.EQ?.eq eq`
(CE.CM?.mult m (select hd am) (xsdenote eq m am tl)))
= let open FStar.Algebra.CommMonoid.Equiv in
match tl with
| [] ->
assert (xsdenote eq m am (hd::tl) == select hd am);
CM?.identity m (select hd am);
EQ?.symmetry eq (CM?.unit m `CM?.mult m` select hd am) (select hd am);
CM?.commutativity m (CM?.unit m) (select hd am);
EQ?.transitivity eq
(xsdenote eq m am (hd::tl))
(CM?.unit m `CM?.mult m` select hd am)
(CM?.mult m (select hd am) (xsdenote eq m am tl))
| _ -> EQ?.reflexivity eq (xsdenote eq m am (hd::tl))
let rec partition_equiv (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (pivot:atom) (q:list atom)
: Lemma
(let open FStar.List.Tot.Base in
let hi, lo = partition (bool_of_compare (compare_of_bool (<)) pivot) q in
CE.EQ?.eq eq
(xsdenote eq m am hi `CE.CM?.mult m` xsdenote eq m am lo)
(xsdenote eq m am q))
= let open FStar.Algebra.CommMonoid.Equiv in
let open FStar.List.Tot.Base in
let f = bool_of_compare (compare_of_bool (<)) pivot in
let hi, lo = partition f q in
match q with
| [] -> CM?.identity m (xsdenote eq m am hi)
| hd::tl ->
let l1, l2 = partition f tl in
partition_equiv eq m am pivot tl;
assert (EQ?.eq eq
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(xsdenote eq m am tl));
EQ?.reflexivity eq (xsdenote eq m am l1);
EQ?.reflexivity eq (xsdenote eq m am l2);
EQ?.reflexivity eq (xsdenote eq m am hi);
EQ?.reflexivity eq (xsdenote eq m am lo);
if f hd then begin
assert (hi == hd::l1 /\ lo == l2);
lemma_xsdenote_aux eq m am hd l1;
CM?.congruence m
(xsdenote eq m am hi)
(xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am l1)
(xsdenote eq m am l2);
CM?.associativity m
(select hd am)
(xsdenote eq m am l1)
(xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
((select hd am `CM?.mult m` xsdenote eq m am l1) `CM?.mult m` xsdenote eq m am l2)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2));
EQ?.reflexivity eq (select hd am);
CM?.congruence m
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(select hd am)
(xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2))
(select hd am `CM?.mult m` xsdenote eq m am tl);
lemma_xsdenote_aux eq m am hd tl;
EQ?.symmetry eq
(xsdenote eq m am (hd::tl))
(select hd am `CM?.mult m` xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am tl)
(xsdenote eq m am (hd::tl))
end else begin
assert (hi == l1 /\ lo == hd::l2);
lemma_xsdenote_aux eq m am hd l2;
CM?.congruence m
(xsdenote eq m am hi)
(xsdenote eq m am lo)
(xsdenote eq m am l1)
(select hd am `CM?.mult m` xsdenote eq m am l2);
CM?.commutativity m
(xsdenote eq m am l1)
(select hd am `CM?.mult m` xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(xsdenote eq m am l1 `CM?.mult m` (select hd am `CM?.mult m` xsdenote eq m am l2))
((select hd am `CM?.mult m` xsdenote eq m am l2) `CM?.mult m` xsdenote eq m am l1);
CM?.associativity m
(select hd am)
(xsdenote eq m am l2)
(xsdenote eq m am l1);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
((select hd am `CM?.mult m` xsdenote eq m am l2) `CM?.mult m` xsdenote eq m am l1)
(select hd am `CM?.mult m` (xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1));
CM?.commutativity m (xsdenote eq m am l2) (xsdenote eq m am l1);
EQ?.reflexivity eq (select hd am);
CM?.congruence m
(select hd am)
(xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1)
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1))
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2));
CM?.congruence m
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(select hd am)
(xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2))
(select hd am `CM?.mult m` xsdenote eq m am tl);
lemma_xsdenote_aux eq m am hd tl;
EQ?.symmetry eq
(xsdenote eq m am (hd::tl))
(select hd am `CM?.mult m` xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am tl)
(xsdenote eq m am (hd::tl))
end
let rec sort_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom)
: Lemma (requires True)
(ensures xsdenote eq m am xs `CE.EQ?.eq eq` xsdenote eq m am (sort xs))
(decreases (FStar.List.Tot.Base.length xs))
= let open FStar.Algebra.CommMonoid.Equiv in
match xs with
| [] -> EQ?.reflexivity eq (xsdenote eq m am [])
| pivot::q ->
let sort0 : permute = List.Tot.sortWith #int (List.Tot.compare_of_bool (<)) in
let sort_eq (l: list atom) : Lemma
(sort l == sort0 l)
[SMTPat (sort l)]
= sortWith_ext (my_compare_of_bool (<)) (List.Tot.compare_of_bool (<)) l
in
let open FStar.List.Tot.Base in
let f:int -> int -> int = compare_of_bool (<) in
let hi, lo = partition (bool_of_compare f pivot) q in
flatten_correct_aux eq m am (sort lo) (pivot::sort hi);
assert (xsdenote eq m am (sort xs) `EQ?.eq eq`
CM?.mult m (xsdenote eq m am (sort lo))
(xsdenote eq m am (pivot::sort hi)));
lemma_xsdenote_aux eq m am pivot (sort hi);
EQ?.reflexivity eq (xsdenote eq m am (sort lo));
CM?.congruence m
(xsdenote eq m am (sort lo))
(xsdenote eq m am (pivot::sort hi))
(xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` xsdenote eq m am (sort hi));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` xsdenote eq m am (pivot::sort hi))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi))));
CM?.commutativity m
(xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` xsdenote eq m am (sort hi));
CM?.associativity m
(select pivot am)
(xsdenote eq m am (sort hi))
(xsdenote eq m am (sort lo));
EQ?.transitivity eq
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)))
((select pivot am `CM?.mult m` xsdenote eq m am (sort hi)) `CM?.mult m` xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))));
partition_length (bool_of_compare f pivot) q;
sort_correct_aux eq m am hi;
sort_correct_aux eq m am lo;
EQ?.symmetry eq (xsdenote eq m am lo) (xsdenote eq m am (sort lo));
EQ?.symmetry eq (xsdenote eq m am hi) (xsdenote eq m am (sort hi));
CM?.congruence m
(xsdenote eq m am (sort hi))
(xsdenote eq m am (sort lo))
(xsdenote eq m am hi)
(xsdenote eq m am lo);
assert (EQ?.eq eq
(xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo));
EQ?.reflexivity eq (select pivot am);
CM?.congruence m
(select pivot am)
(xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))
(select pivot am)
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo);
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)));
partition_equiv eq m am pivot q;
CM?.congruence m
(select pivot am)
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select pivot am)
(xsdenote eq m am q);
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo))
(select pivot am `CM?.mult m` (xsdenote eq m am q));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am q)));
lemma_xsdenote_aux eq m am pivot q;
EQ?.symmetry eq
(xsdenote eq m am (pivot::q))
(select pivot am `CM?.mult m` (xsdenote eq m am q));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am q))
(xsdenote eq m am xs);
EQ?.symmetry eq (xsdenote eq m am (sort xs)) (xsdenote eq m am xs)
#pop-options
#push-options "--fuel 0 --ifuel 0"
(* Lemmas to be called after a permutation compatible with AC-unification was found *)
let smt_reflexivity (#a:Type) (eq:CE.equiv a) (x y:a)
: Lemma (requires x == y)
(ensures CE.EQ?.eq eq x y)
= CE.EQ?.reflexivity eq x
let identity_left_smt (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x y:a)
: Lemma
(requires x == y)
(ensures CE.EQ?.eq eq x (CE.CM?.mult m (CE.CM?.unit m) y))
= CE.CM?.identity m x;
CE.EQ?.symmetry eq (CE.CM?.mult m (CE.CM?.unit m) x) x
let identity_left (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x:a)
: Lemma (CE.EQ?.eq eq x (CE.CM?.mult m (CE.CM?.unit m) x))
= CE.CM?.identity m x;
CE.EQ?.symmetry eq (CE.CM?.mult m (CE.CM?.unit m) x) x
let identity_right_diff (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x y:a) : Lemma
(requires CE.EQ?.eq eq x y)
(ensures CE.EQ?.eq eq (CE.CM?.mult m x (CE.CM?.unit m)) y)
= CE.right_identity eq m x;
CE.EQ?.transitivity eq (CE.CM?.mult m x (CE.CM?.unit m)) x y
/// Dismiss possible vprops goals that might have been created by lemma application.
/// These vprops will be instantiated at a later stage; else, Meta-F* will raise an error
let rec dismiss_slprops () : Tac unit =
match term_as_formula' (cur_goal ()) with
| App t _ -> if is_fvar t (`%squash) then () else (dismiss(); dismiss_slprops ())
| _ -> dismiss(); dismiss_slprops ()
/// Recursively removing trailing empty assertions
let rec n_identity_left (n:int) (eq m:term) : Tac unit
= if n = 0 then (
apply_lemma (`(CE.EQ?.reflexivity (`#eq)));
// Cleaning up, in case a uvar has been generated here. It'll be solved later
set_goals [])
else (
apply_lemma (`identity_right_diff (`#eq) (`#m));
// Drop the slprops generated, they will be solved later
dismiss_slprops ();
n_identity_left (n-1) eq m
)
/// Helper lemma: If two vprops (as represented by lists of atoms) are equivalent, then their canonical forms
/// (corresponding to applying the sort function on atoms) are equivalent
let equivalent_sorted (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (l1 l2 l1' l2':list atom)
: Lemma (requires
sort l1 == sort l1' /\
sort l2 == sort l2' /\
xsdenote eq m am l1 `CE.EQ?.eq eq` xsdenote eq m am l2)
(ensures xsdenote eq m am l1' `CE.EQ?.eq eq` xsdenote eq m am l2')
= let open FStar.Algebra.CommMonoid.Equiv in
sort_correct_aux eq m am l1';
sort_correct_aux eq m am l1;
EQ?.symmetry eq (xsdenote eq m am l1) (xsdenote eq m am (sort l1));
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am (sort l1'))
(xsdenote eq m am l1);
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am l1)
(xsdenote eq m am l2);
sort_correct_aux eq m am l2;
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am l2)
(xsdenote eq m am (sort l2));
sort_correct_aux eq m am l2';
EQ?.symmetry eq (xsdenote eq m am l2') (xsdenote eq m am (sort l2'));
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am (sort l2))
(xsdenote eq m am l2')
#pop-options
/// Finds the position of first occurrence of x in xs.
/// This is now specialized to terms and their funny term_eq_old.
let rec where_aux (n:nat) (x:term) (xs:list term) :
Tac (option nat) (decreases xs) =
match xs with
| [] -> None | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Reflection.V2.Derived.Lemmas",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: FStar.Tactics.NamedView.term -> xs: Prims.list FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac (FStar.Pervasives.Native.option Prims.nat) | FStar.Tactics.Effect.Tac | [
""
] | [] | [
"Steel.Effect.Common.where_aux"
] | [] | false | true | false | false | false | let where =
| where_aux 0 | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.n_identity_left | val n_identity_left (n: int) (eq m: term) : Tac unit | val n_identity_left (n: int) (eq m: term) : Tac unit | let rec n_identity_left (n:int) (eq m:term) : Tac unit
= if n = 0 then (
apply_lemma (`(CE.EQ?.reflexivity (`#eq)));
// Cleaning up, in case a uvar has been generated here. It'll be solved later
set_goals [])
else (
apply_lemma (`identity_right_diff (`#eq) (`#m));
// Drop the slprops generated, they will be solved later
dismiss_slprops ();
n_identity_left (n-1) eq m
) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 5,
"end_line": 1570,
"start_col": 0,
"start_line": 1560
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am
/// Iterates over all terms in [l2] to prepare them for unification with SMT rewriting
let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2
/// Recursively calls equivalent_lists_once.
/// Stops when we're done with unification, or when we didn't make any progress
/// If we didn't make any progress, we have too many candidates for some terms.
/// Accumulates rewritings of l1 and l2 in l1_del and l2_del, with the invariant
/// that the two lists are unifiable at any point
/// The boolean indicates if there is a leftover empty frame
let rec equivalent_lists' (n:nat) (use_smt:bool) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, []
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Try to make progress by matching non-uvars of l2 with candidates in l1
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Should always be smaller or equal to n
// If it is equal, no progress was made.
if use_smt then
// SMT fallback is allowed
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
/// Checks if term for atom t unifies with fall uvars in l
let rec unifies_with_all_uvars (t:term) (l:list atom) (am:amap term) : Tac bool =
match l with
| [] -> true
| hd::tl ->
if unifies_with_all_uvars t tl am then (
// Unified with tail, try this term
let hd_t = select hd am in
if is_uvar hd_t then (
// The head term is a uvar, try unifying
try if unify t hd_t then raise Success else raise Failed
with | Success -> true | _ -> false
) else true // The uvar is not a head term, we do not need to try it
) else false
/// Puts all terms in l1 that cannot unify with the uvars in l2 at the top:
/// They need to be solved first
let rec most_restricted_at_top (l1 l2:list atom) (am:amap term) : Tac (list atom) =
match l1 with
| [] -> []
| hd::tl ->
if unifies_with_all_uvars (select hd am) l2 am then (most_restricted_at_top tl l2 am) `List.Tot.append` [hd]
else hd::(most_restricted_at_top tl l2 am)
/// Core AC-unification tactic.
/// First remove all trivially equal terms, then try to decide equivalence.
/// Assumes that l1 does not contain any vprop uvar.
/// If it succeeds, returns permutations of l1, l2, and a boolean indicating
/// if l2 has a trailing empty frame to be unified
let equivalent_lists (use_smt:bool) (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term)
= let l1, l2, l1_del, l2_del = trivial_cancels l1 l2 am in
let l1 = most_restricted_at_top l1 l2 am in
let n = List.Tot.length l1 in
let l1_del, l2_del, emp_frame, uvar_terms = equivalent_lists' n use_smt l1 l2 l1_del l2_del am in
l1_del, l2_del, emp_frame, uvar_terms
(* Helpers to relate the actual terms to their representation as a list of atoms *)
open FStar.Reflection.V2.Derived.Lemmas
let rec list_to_string (l:list term) : Tac string =
match l with
| [] -> "end"
| hd::tl -> term_to_string hd ^ " " ^ list_to_string tl
let rec mdenote_gen (#a:Type u#aa) (unit:a) (mult:a -> a -> a) (am:amap a) (e:exp) : a =
match e with
| Unit -> unit
| Atom x -> select x am
| Mult e1 e2 -> mult (mdenote_gen unit mult am e1) (mdenote_gen unit mult am e2)
let rec xsdenote_gen (#a:Type) (unit:a) (mult:a -> a -> a) (am:amap a) (xs:list atom) : a =
match xs with
| [] -> unit
| [x] -> select x am
| x::xs' -> mult (select x am) (xsdenote_gen unit mult am xs')
unfold
let mdenote (#a:Type u#aa) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp) : a =
let open FStar.Algebra.CommMonoid.Equiv in
mdenote_gen (CM?.unit m) (CM?.mult m) am e
unfold
let xsdenote (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom) : a =
let open FStar.Algebra.CommMonoid.Equiv in
xsdenote_gen (CM?.unit m) (CM?.mult m) am xs
// We reimplement List.Tot.Base.append because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_append (#t: Type) (l1 l2: list t) : Pure (list t)
(requires True)
(ensures (fun res -> res == l1 `List.Tot.append` l2))
(decreases l1)
= match l1 with
| [] -> l2
| a :: q -> a :: my_append q l2
let rec flatten (e:exp) : list atom =
match e with
| Unit -> []
| Atom x -> [x]
| Mult e1 e2 -> flatten e1 `my_append` flatten e2
let rec flatten_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs1 xs2:list atom)
: Lemma (xsdenote eq m am (xs1 `my_append` xs2) `CE.EQ?.eq eq` CE.CM?.mult m (xsdenote eq m am xs1)
(xsdenote eq m am xs2)) =
let open FStar.Algebra.CommMonoid.Equiv in
match xs1 with
| [] ->
CM?.identity m (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.unit m) (xsdenote eq m am xs2)) (xsdenote eq m am xs2)
| [x] -> (
if (Nil? xs2)
then (right_identity eq m (select x am);
EQ?.symmetry eq (CM?.mult m (select x am) (CM?.unit m)) (select x am))
else EQ?.reflexivity eq (CM?.mult m (xsdenote eq m am [x]) (xsdenote eq m am xs2)))
| x::xs1' ->
flatten_correct_aux eq m am xs1' xs2;
EQ?.reflexivity eq (select x am);
CM?.congruence m (select x am) (xsdenote eq m am (xs1' `my_append` xs2))
(select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2));
CM?.associativity m (select x am) (xsdenote eq m am xs1') (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)));
EQ?.transitivity eq (CM?.mult m (select x am) (xsdenote eq m am (xs1' `my_append` xs2)))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)))
(CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
let rec flatten_correct (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp)
: Lemma (mdenote eq m am e `CE.EQ?.eq eq` xsdenote eq m am (flatten e)) =
let open FStar.Algebra.CommMonoid.Equiv in
match e with
| Unit -> EQ?.reflexivity eq (CM?.unit m)
| Atom x -> EQ?.reflexivity eq (select x am)
| Mult e1 e2 ->
flatten_correct_aux eq m am (flatten e1) (flatten e2);
EQ?.symmetry eq (xsdenote eq m am (flatten e1 `my_append` flatten e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)));
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CM?.congruence m (mdenote eq m am e1) (mdenote eq m am e2)
(xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2));
EQ?.transitivity eq (CM?.mult m (mdenote eq m am e1) (mdenote eq m am e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)))
(xsdenote eq m am (flatten e1 `my_append` flatten e2))
let monoid_reflect (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e1 e2:exp)
(_ : squash (xsdenote eq m am (flatten e1) `CE.EQ?.eq eq` xsdenote eq m am (flatten e2)))
: squash (mdenote eq m am e1 `CE.EQ?.eq eq` mdenote eq m am e2) =
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CE.EQ?.symmetry eq (mdenote eq m am e2) (xsdenote eq m am (flatten e2));
CE.EQ?.transitivity eq
(xsdenote eq m am (flatten e1))
(xsdenote eq m am (flatten e2))
(mdenote eq m am e2);
CE.EQ?.transitivity eq
(mdenote eq m am e1)
(xsdenote eq m am (flatten e1))
(mdenote eq m am e2)
// Here we sort the variable numbers
// We reimplement List.Tot.Base.sortWith because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_partition (#a: Type) (f: (a -> Tot bool)) (l: list a)
: Pure (list a & list a)
(requires True)
(ensures (fun res -> res == List.Tot.partition f l))
= match l with
| [] -> [], []
| hd::tl ->
let l1, l2 = my_partition f tl in
if f hd
then hd::l1, l2
else l1, hd::l2
let rec partition_ext (#a: Type) (f1 f2: (a -> Tot bool)) (l: list a)
: Lemma
(requires (forall x . f1 x == f2 x))
(ensures (List.Tot.partition f1 l == List.Tot.partition f2 l))
= match l with
| [] -> ()
| hd::tl -> partition_ext f1 f2 tl
let my_bool_of_compare (#a: Type) (f: a -> a -> Tot int) (x: a) (y: a) : Tot bool
= f x y < 0
let rec my_sortWith (#a: Type) (f: (a -> a -> Tot int)) (l:list a)
: Pure (list a)
(requires True)
(ensures (fun res -> res == List.Tot.sortWith f l))
(decreases (List.Tot.length l))
= match l with
| [] -> []
| pivot::tl ->
let hi, lo = my_partition (my_bool_of_compare f pivot) tl in
partition_ext (my_bool_of_compare f pivot) (List.Tot.bool_of_compare f pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f pivot) tl;
my_append (my_sortWith f lo) (pivot::my_sortWith f hi)
let rec sortWith_ext (#a: Type) (f1 f2: (a -> a -> Tot int)) (l: list a)
: Lemma
(requires (forall x y . f1 x y == f2 x y))
(ensures (List.Tot.sortWith f1 l == List.Tot.sortWith f2 l))
(decreases (List.Tot.length l))
= match l with
| [] -> ()
| pivot::tl ->
partition_ext (List.Tot.bool_of_compare f1 pivot) (List.Tot.bool_of_compare f2 pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f1 pivot) tl;
let hi, lo = List.Tot.partition (List.Tot.bool_of_compare f1 pivot) tl in
sortWith_ext f1 f2 lo;
sortWith_ext f1 f2 hi
let permute = list atom -> list atom
let my_compare_of_bool (#a:eqtype) (rel: a -> a -> Tot bool) (x: a) (y: a) : Tot int
= if x `rel` y then -1
else if x = y then 0
else 1
let sort : permute = my_sortWith #int (my_compare_of_bool (<))
#push-options "--fuel 1 --ifuel 1"
let lemma_xsdenote_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (hd:atom) (tl:list atom)
: Lemma (xsdenote eq m am (hd::tl) `CE.EQ?.eq eq`
(CE.CM?.mult m (select hd am) (xsdenote eq m am tl)))
= let open FStar.Algebra.CommMonoid.Equiv in
match tl with
| [] ->
assert (xsdenote eq m am (hd::tl) == select hd am);
CM?.identity m (select hd am);
EQ?.symmetry eq (CM?.unit m `CM?.mult m` select hd am) (select hd am);
CM?.commutativity m (CM?.unit m) (select hd am);
EQ?.transitivity eq
(xsdenote eq m am (hd::tl))
(CM?.unit m `CM?.mult m` select hd am)
(CM?.mult m (select hd am) (xsdenote eq m am tl))
| _ -> EQ?.reflexivity eq (xsdenote eq m am (hd::tl))
let rec partition_equiv (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (pivot:atom) (q:list atom)
: Lemma
(let open FStar.List.Tot.Base in
let hi, lo = partition (bool_of_compare (compare_of_bool (<)) pivot) q in
CE.EQ?.eq eq
(xsdenote eq m am hi `CE.CM?.mult m` xsdenote eq m am lo)
(xsdenote eq m am q))
= let open FStar.Algebra.CommMonoid.Equiv in
let open FStar.List.Tot.Base in
let f = bool_of_compare (compare_of_bool (<)) pivot in
let hi, lo = partition f q in
match q with
| [] -> CM?.identity m (xsdenote eq m am hi)
| hd::tl ->
let l1, l2 = partition f tl in
partition_equiv eq m am pivot tl;
assert (EQ?.eq eq
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(xsdenote eq m am tl));
EQ?.reflexivity eq (xsdenote eq m am l1);
EQ?.reflexivity eq (xsdenote eq m am l2);
EQ?.reflexivity eq (xsdenote eq m am hi);
EQ?.reflexivity eq (xsdenote eq m am lo);
if f hd then begin
assert (hi == hd::l1 /\ lo == l2);
lemma_xsdenote_aux eq m am hd l1;
CM?.congruence m
(xsdenote eq m am hi)
(xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am l1)
(xsdenote eq m am l2);
CM?.associativity m
(select hd am)
(xsdenote eq m am l1)
(xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
((select hd am `CM?.mult m` xsdenote eq m am l1) `CM?.mult m` xsdenote eq m am l2)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2));
EQ?.reflexivity eq (select hd am);
CM?.congruence m
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(select hd am)
(xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2))
(select hd am `CM?.mult m` xsdenote eq m am tl);
lemma_xsdenote_aux eq m am hd tl;
EQ?.symmetry eq
(xsdenote eq m am (hd::tl))
(select hd am `CM?.mult m` xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am tl)
(xsdenote eq m am (hd::tl))
end else begin
assert (hi == l1 /\ lo == hd::l2);
lemma_xsdenote_aux eq m am hd l2;
CM?.congruence m
(xsdenote eq m am hi)
(xsdenote eq m am lo)
(xsdenote eq m am l1)
(select hd am `CM?.mult m` xsdenote eq m am l2);
CM?.commutativity m
(xsdenote eq m am l1)
(select hd am `CM?.mult m` xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(xsdenote eq m am l1 `CM?.mult m` (select hd am `CM?.mult m` xsdenote eq m am l2))
((select hd am `CM?.mult m` xsdenote eq m am l2) `CM?.mult m` xsdenote eq m am l1);
CM?.associativity m
(select hd am)
(xsdenote eq m am l2)
(xsdenote eq m am l1);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
((select hd am `CM?.mult m` xsdenote eq m am l2) `CM?.mult m` xsdenote eq m am l1)
(select hd am `CM?.mult m` (xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1));
CM?.commutativity m (xsdenote eq m am l2) (xsdenote eq m am l1);
EQ?.reflexivity eq (select hd am);
CM?.congruence m
(select hd am)
(xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1)
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1))
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2));
CM?.congruence m
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(select hd am)
(xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2))
(select hd am `CM?.mult m` xsdenote eq m am tl);
lemma_xsdenote_aux eq m am hd tl;
EQ?.symmetry eq
(xsdenote eq m am (hd::tl))
(select hd am `CM?.mult m` xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am tl)
(xsdenote eq m am (hd::tl))
end
let rec sort_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom)
: Lemma (requires True)
(ensures xsdenote eq m am xs `CE.EQ?.eq eq` xsdenote eq m am (sort xs))
(decreases (FStar.List.Tot.Base.length xs))
= let open FStar.Algebra.CommMonoid.Equiv in
match xs with
| [] -> EQ?.reflexivity eq (xsdenote eq m am [])
| pivot::q ->
let sort0 : permute = List.Tot.sortWith #int (List.Tot.compare_of_bool (<)) in
let sort_eq (l: list atom) : Lemma
(sort l == sort0 l)
[SMTPat (sort l)]
= sortWith_ext (my_compare_of_bool (<)) (List.Tot.compare_of_bool (<)) l
in
let open FStar.List.Tot.Base in
let f:int -> int -> int = compare_of_bool (<) in
let hi, lo = partition (bool_of_compare f pivot) q in
flatten_correct_aux eq m am (sort lo) (pivot::sort hi);
assert (xsdenote eq m am (sort xs) `EQ?.eq eq`
CM?.mult m (xsdenote eq m am (sort lo))
(xsdenote eq m am (pivot::sort hi)));
lemma_xsdenote_aux eq m am pivot (sort hi);
EQ?.reflexivity eq (xsdenote eq m am (sort lo));
CM?.congruence m
(xsdenote eq m am (sort lo))
(xsdenote eq m am (pivot::sort hi))
(xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` xsdenote eq m am (sort hi));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` xsdenote eq m am (pivot::sort hi))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi))));
CM?.commutativity m
(xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` xsdenote eq m am (sort hi));
CM?.associativity m
(select pivot am)
(xsdenote eq m am (sort hi))
(xsdenote eq m am (sort lo));
EQ?.transitivity eq
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)))
((select pivot am `CM?.mult m` xsdenote eq m am (sort hi)) `CM?.mult m` xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))));
partition_length (bool_of_compare f pivot) q;
sort_correct_aux eq m am hi;
sort_correct_aux eq m am lo;
EQ?.symmetry eq (xsdenote eq m am lo) (xsdenote eq m am (sort lo));
EQ?.symmetry eq (xsdenote eq m am hi) (xsdenote eq m am (sort hi));
CM?.congruence m
(xsdenote eq m am (sort hi))
(xsdenote eq m am (sort lo))
(xsdenote eq m am hi)
(xsdenote eq m am lo);
assert (EQ?.eq eq
(xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo));
EQ?.reflexivity eq (select pivot am);
CM?.congruence m
(select pivot am)
(xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))
(select pivot am)
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo);
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)));
partition_equiv eq m am pivot q;
CM?.congruence m
(select pivot am)
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select pivot am)
(xsdenote eq m am q);
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo))
(select pivot am `CM?.mult m` (xsdenote eq m am q));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am q)));
lemma_xsdenote_aux eq m am pivot q;
EQ?.symmetry eq
(xsdenote eq m am (pivot::q))
(select pivot am `CM?.mult m` (xsdenote eq m am q));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am q))
(xsdenote eq m am xs);
EQ?.symmetry eq (xsdenote eq m am (sort xs)) (xsdenote eq m am xs)
#pop-options
#push-options "--fuel 0 --ifuel 0"
(* Lemmas to be called after a permutation compatible with AC-unification was found *)
let smt_reflexivity (#a:Type) (eq:CE.equiv a) (x y:a)
: Lemma (requires x == y)
(ensures CE.EQ?.eq eq x y)
= CE.EQ?.reflexivity eq x
let identity_left_smt (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x y:a)
: Lemma
(requires x == y)
(ensures CE.EQ?.eq eq x (CE.CM?.mult m (CE.CM?.unit m) y))
= CE.CM?.identity m x;
CE.EQ?.symmetry eq (CE.CM?.mult m (CE.CM?.unit m) x) x
let identity_left (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x:a)
: Lemma (CE.EQ?.eq eq x (CE.CM?.mult m (CE.CM?.unit m) x))
= CE.CM?.identity m x;
CE.EQ?.symmetry eq (CE.CM?.mult m (CE.CM?.unit m) x) x
let identity_right_diff (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x y:a) : Lemma
(requires CE.EQ?.eq eq x y)
(ensures CE.EQ?.eq eq (CE.CM?.mult m x (CE.CM?.unit m)) y)
= CE.right_identity eq m x;
CE.EQ?.transitivity eq (CE.CM?.mult m x (CE.CM?.unit m)) x y
/// Dismiss possible vprops goals that might have been created by lemma application.
/// These vprops will be instantiated at a later stage; else, Meta-F* will raise an error
let rec dismiss_slprops () : Tac unit =
match term_as_formula' (cur_goal ()) with
| App t _ -> if is_fvar t (`%squash) then () else (dismiss(); dismiss_slprops ())
| _ -> dismiss(); dismiss_slprops () | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Reflection.V2.Derived.Lemmas",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | n: Prims.int -> eq: FStar.Tactics.NamedView.term -> m: FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac Prims.unit | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.int",
"FStar.Tactics.NamedView.term",
"Prims.op_Equality",
"FStar.Stubs.Tactics.V2.Builtins.set_goals",
"Prims.Nil",
"FStar.Stubs.Tactics.Types.goal",
"Prims.unit",
"FStar.Tactics.V2.Derived.apply_lemma",
"Prims.bool",
"Steel.Effect.Common.n_identity_left",
"Prims.op_Subtraction",
"Steel.Effect.Common.dismiss_slprops"
] | [
"recursion"
] | false | true | false | false | false | let rec n_identity_left (n: int) (eq m: term) : Tac unit =
| if n = 0
then
(apply_lemma (`(CE.EQ?.reflexivity (`#eq)));
set_goals [])
else
(apply_lemma (`identity_right_diff (`#eq) (`#m));
dismiss_slprops ();
n_identity_left (n - 1) eq m) | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.get_candidates | val get_candidates (t: term) (l: list term) : Tac (list term) | val get_candidates (t: term) (l: list term) : Tac (list term) | let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 32,
"end_line": 804,
"start_col": 0,
"start_line": 796
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | t: FStar.Tactics.NamedView.term -> l: Prims.list FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac (Prims.list FStar.Tactics.NamedView.term) | FStar.Tactics.Effect.Tac | [] | [] | [
"FStar.Tactics.NamedView.term",
"Prims.list",
"FStar.Stubs.Reflection.V2.Data.argv",
"Prims.Nil",
"Prims.Cons",
"Steel.Effect.Common.get_candidates",
"Prims.bool",
"FStar.Stubs.Tactics.V2.Builtins.term_eq_old",
"FStar.Pervasives.Native.tuple2",
"FStar.Tactics.V2.SyntaxHelpers.collect_app"
] | [
"recursion"
] | false | true | false | false | false | let rec get_candidates (t: term) (l: list term) : Tac (list term) =
| let name, _ = collect_app t in
match l with
| [] -> []
| hd :: tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (hd :: (get_candidates t tl)) else get_candidates t tl | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.dismiss_slprops | val dismiss_slprops: Prims.unit -> Tac unit | val dismiss_slprops: Prims.unit -> Tac unit | let rec dismiss_slprops () : Tac unit =
match term_as_formula' (cur_goal ()) with
| App t _ -> if is_fvar t (`%squash) then () else (dismiss(); dismiss_slprops ())
| _ -> dismiss(); dismiss_slprops () | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 40,
"end_line": 1557,
"start_col": 0,
"start_line": 1554
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am
/// Iterates over all terms in [l2] to prepare them for unification with SMT rewriting
let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2
/// Recursively calls equivalent_lists_once.
/// Stops when we're done with unification, or when we didn't make any progress
/// If we didn't make any progress, we have too many candidates for some terms.
/// Accumulates rewritings of l1 and l2 in l1_del and l2_del, with the invariant
/// that the two lists are unifiable at any point
/// The boolean indicates if there is a leftover empty frame
let rec equivalent_lists' (n:nat) (use_smt:bool) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, []
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Try to make progress by matching non-uvars of l2 with candidates in l1
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Should always be smaller or equal to n
// If it is equal, no progress was made.
if use_smt then
// SMT fallback is allowed
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
/// Checks if term for atom t unifies with fall uvars in l
let rec unifies_with_all_uvars (t:term) (l:list atom) (am:amap term) : Tac bool =
match l with
| [] -> true
| hd::tl ->
if unifies_with_all_uvars t tl am then (
// Unified with tail, try this term
let hd_t = select hd am in
if is_uvar hd_t then (
// The head term is a uvar, try unifying
try if unify t hd_t then raise Success else raise Failed
with | Success -> true | _ -> false
) else true // The uvar is not a head term, we do not need to try it
) else false
/// Puts all terms in l1 that cannot unify with the uvars in l2 at the top:
/// They need to be solved first
let rec most_restricted_at_top (l1 l2:list atom) (am:amap term) : Tac (list atom) =
match l1 with
| [] -> []
| hd::tl ->
if unifies_with_all_uvars (select hd am) l2 am then (most_restricted_at_top tl l2 am) `List.Tot.append` [hd]
else hd::(most_restricted_at_top tl l2 am)
/// Core AC-unification tactic.
/// First remove all trivially equal terms, then try to decide equivalence.
/// Assumes that l1 does not contain any vprop uvar.
/// If it succeeds, returns permutations of l1, l2, and a boolean indicating
/// if l2 has a trailing empty frame to be unified
let equivalent_lists (use_smt:bool) (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term)
= let l1, l2, l1_del, l2_del = trivial_cancels l1 l2 am in
let l1 = most_restricted_at_top l1 l2 am in
let n = List.Tot.length l1 in
let l1_del, l2_del, emp_frame, uvar_terms = equivalent_lists' n use_smt l1 l2 l1_del l2_del am in
l1_del, l2_del, emp_frame, uvar_terms
(* Helpers to relate the actual terms to their representation as a list of atoms *)
open FStar.Reflection.V2.Derived.Lemmas
let rec list_to_string (l:list term) : Tac string =
match l with
| [] -> "end"
| hd::tl -> term_to_string hd ^ " " ^ list_to_string tl
let rec mdenote_gen (#a:Type u#aa) (unit:a) (mult:a -> a -> a) (am:amap a) (e:exp) : a =
match e with
| Unit -> unit
| Atom x -> select x am
| Mult e1 e2 -> mult (mdenote_gen unit mult am e1) (mdenote_gen unit mult am e2)
let rec xsdenote_gen (#a:Type) (unit:a) (mult:a -> a -> a) (am:amap a) (xs:list atom) : a =
match xs with
| [] -> unit
| [x] -> select x am
| x::xs' -> mult (select x am) (xsdenote_gen unit mult am xs')
unfold
let mdenote (#a:Type u#aa) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp) : a =
let open FStar.Algebra.CommMonoid.Equiv in
mdenote_gen (CM?.unit m) (CM?.mult m) am e
unfold
let xsdenote (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom) : a =
let open FStar.Algebra.CommMonoid.Equiv in
xsdenote_gen (CM?.unit m) (CM?.mult m) am xs
// We reimplement List.Tot.Base.append because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_append (#t: Type) (l1 l2: list t) : Pure (list t)
(requires True)
(ensures (fun res -> res == l1 `List.Tot.append` l2))
(decreases l1)
= match l1 with
| [] -> l2
| a :: q -> a :: my_append q l2
let rec flatten (e:exp) : list atom =
match e with
| Unit -> []
| Atom x -> [x]
| Mult e1 e2 -> flatten e1 `my_append` flatten e2
let rec flatten_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs1 xs2:list atom)
: Lemma (xsdenote eq m am (xs1 `my_append` xs2) `CE.EQ?.eq eq` CE.CM?.mult m (xsdenote eq m am xs1)
(xsdenote eq m am xs2)) =
let open FStar.Algebra.CommMonoid.Equiv in
match xs1 with
| [] ->
CM?.identity m (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.unit m) (xsdenote eq m am xs2)) (xsdenote eq m am xs2)
| [x] -> (
if (Nil? xs2)
then (right_identity eq m (select x am);
EQ?.symmetry eq (CM?.mult m (select x am) (CM?.unit m)) (select x am))
else EQ?.reflexivity eq (CM?.mult m (xsdenote eq m am [x]) (xsdenote eq m am xs2)))
| x::xs1' ->
flatten_correct_aux eq m am xs1' xs2;
EQ?.reflexivity eq (select x am);
CM?.congruence m (select x am) (xsdenote eq m am (xs1' `my_append` xs2))
(select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2));
CM?.associativity m (select x am) (xsdenote eq m am xs1') (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)));
EQ?.transitivity eq (CM?.mult m (select x am) (xsdenote eq m am (xs1' `my_append` xs2)))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)))
(CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
let rec flatten_correct (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp)
: Lemma (mdenote eq m am e `CE.EQ?.eq eq` xsdenote eq m am (flatten e)) =
let open FStar.Algebra.CommMonoid.Equiv in
match e with
| Unit -> EQ?.reflexivity eq (CM?.unit m)
| Atom x -> EQ?.reflexivity eq (select x am)
| Mult e1 e2 ->
flatten_correct_aux eq m am (flatten e1) (flatten e2);
EQ?.symmetry eq (xsdenote eq m am (flatten e1 `my_append` flatten e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)));
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CM?.congruence m (mdenote eq m am e1) (mdenote eq m am e2)
(xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2));
EQ?.transitivity eq (CM?.mult m (mdenote eq m am e1) (mdenote eq m am e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)))
(xsdenote eq m am (flatten e1 `my_append` flatten e2))
let monoid_reflect (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e1 e2:exp)
(_ : squash (xsdenote eq m am (flatten e1) `CE.EQ?.eq eq` xsdenote eq m am (flatten e2)))
: squash (mdenote eq m am e1 `CE.EQ?.eq eq` mdenote eq m am e2) =
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CE.EQ?.symmetry eq (mdenote eq m am e2) (xsdenote eq m am (flatten e2));
CE.EQ?.transitivity eq
(xsdenote eq m am (flatten e1))
(xsdenote eq m am (flatten e2))
(mdenote eq m am e2);
CE.EQ?.transitivity eq
(mdenote eq m am e1)
(xsdenote eq m am (flatten e1))
(mdenote eq m am e2)
// Here we sort the variable numbers
// We reimplement List.Tot.Base.sortWith because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_partition (#a: Type) (f: (a -> Tot bool)) (l: list a)
: Pure (list a & list a)
(requires True)
(ensures (fun res -> res == List.Tot.partition f l))
= match l with
| [] -> [], []
| hd::tl ->
let l1, l2 = my_partition f tl in
if f hd
then hd::l1, l2
else l1, hd::l2
let rec partition_ext (#a: Type) (f1 f2: (a -> Tot bool)) (l: list a)
: Lemma
(requires (forall x . f1 x == f2 x))
(ensures (List.Tot.partition f1 l == List.Tot.partition f2 l))
= match l with
| [] -> ()
| hd::tl -> partition_ext f1 f2 tl
let my_bool_of_compare (#a: Type) (f: a -> a -> Tot int) (x: a) (y: a) : Tot bool
= f x y < 0
let rec my_sortWith (#a: Type) (f: (a -> a -> Tot int)) (l:list a)
: Pure (list a)
(requires True)
(ensures (fun res -> res == List.Tot.sortWith f l))
(decreases (List.Tot.length l))
= match l with
| [] -> []
| pivot::tl ->
let hi, lo = my_partition (my_bool_of_compare f pivot) tl in
partition_ext (my_bool_of_compare f pivot) (List.Tot.bool_of_compare f pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f pivot) tl;
my_append (my_sortWith f lo) (pivot::my_sortWith f hi)
let rec sortWith_ext (#a: Type) (f1 f2: (a -> a -> Tot int)) (l: list a)
: Lemma
(requires (forall x y . f1 x y == f2 x y))
(ensures (List.Tot.sortWith f1 l == List.Tot.sortWith f2 l))
(decreases (List.Tot.length l))
= match l with
| [] -> ()
| pivot::tl ->
partition_ext (List.Tot.bool_of_compare f1 pivot) (List.Tot.bool_of_compare f2 pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f1 pivot) tl;
let hi, lo = List.Tot.partition (List.Tot.bool_of_compare f1 pivot) tl in
sortWith_ext f1 f2 lo;
sortWith_ext f1 f2 hi
let permute = list atom -> list atom
let my_compare_of_bool (#a:eqtype) (rel: a -> a -> Tot bool) (x: a) (y: a) : Tot int
= if x `rel` y then -1
else if x = y then 0
else 1
let sort : permute = my_sortWith #int (my_compare_of_bool (<))
#push-options "--fuel 1 --ifuel 1"
let lemma_xsdenote_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (hd:atom) (tl:list atom)
: Lemma (xsdenote eq m am (hd::tl) `CE.EQ?.eq eq`
(CE.CM?.mult m (select hd am) (xsdenote eq m am tl)))
= let open FStar.Algebra.CommMonoid.Equiv in
match tl with
| [] ->
assert (xsdenote eq m am (hd::tl) == select hd am);
CM?.identity m (select hd am);
EQ?.symmetry eq (CM?.unit m `CM?.mult m` select hd am) (select hd am);
CM?.commutativity m (CM?.unit m) (select hd am);
EQ?.transitivity eq
(xsdenote eq m am (hd::tl))
(CM?.unit m `CM?.mult m` select hd am)
(CM?.mult m (select hd am) (xsdenote eq m am tl))
| _ -> EQ?.reflexivity eq (xsdenote eq m am (hd::tl))
let rec partition_equiv (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (pivot:atom) (q:list atom)
: Lemma
(let open FStar.List.Tot.Base in
let hi, lo = partition (bool_of_compare (compare_of_bool (<)) pivot) q in
CE.EQ?.eq eq
(xsdenote eq m am hi `CE.CM?.mult m` xsdenote eq m am lo)
(xsdenote eq m am q))
= let open FStar.Algebra.CommMonoid.Equiv in
let open FStar.List.Tot.Base in
let f = bool_of_compare (compare_of_bool (<)) pivot in
let hi, lo = partition f q in
match q with
| [] -> CM?.identity m (xsdenote eq m am hi)
| hd::tl ->
let l1, l2 = partition f tl in
partition_equiv eq m am pivot tl;
assert (EQ?.eq eq
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(xsdenote eq m am tl));
EQ?.reflexivity eq (xsdenote eq m am l1);
EQ?.reflexivity eq (xsdenote eq m am l2);
EQ?.reflexivity eq (xsdenote eq m am hi);
EQ?.reflexivity eq (xsdenote eq m am lo);
if f hd then begin
assert (hi == hd::l1 /\ lo == l2);
lemma_xsdenote_aux eq m am hd l1;
CM?.congruence m
(xsdenote eq m am hi)
(xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am l1)
(xsdenote eq m am l2);
CM?.associativity m
(select hd am)
(xsdenote eq m am l1)
(xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
((select hd am `CM?.mult m` xsdenote eq m am l1) `CM?.mult m` xsdenote eq m am l2)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2));
EQ?.reflexivity eq (select hd am);
CM?.congruence m
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(select hd am)
(xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2))
(select hd am `CM?.mult m` xsdenote eq m am tl);
lemma_xsdenote_aux eq m am hd tl;
EQ?.symmetry eq
(xsdenote eq m am (hd::tl))
(select hd am `CM?.mult m` xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am tl)
(xsdenote eq m am (hd::tl))
end else begin
assert (hi == l1 /\ lo == hd::l2);
lemma_xsdenote_aux eq m am hd l2;
CM?.congruence m
(xsdenote eq m am hi)
(xsdenote eq m am lo)
(xsdenote eq m am l1)
(select hd am `CM?.mult m` xsdenote eq m am l2);
CM?.commutativity m
(xsdenote eq m am l1)
(select hd am `CM?.mult m` xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(xsdenote eq m am l1 `CM?.mult m` (select hd am `CM?.mult m` xsdenote eq m am l2))
((select hd am `CM?.mult m` xsdenote eq m am l2) `CM?.mult m` xsdenote eq m am l1);
CM?.associativity m
(select hd am)
(xsdenote eq m am l2)
(xsdenote eq m am l1);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
((select hd am `CM?.mult m` xsdenote eq m am l2) `CM?.mult m` xsdenote eq m am l1)
(select hd am `CM?.mult m` (xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1));
CM?.commutativity m (xsdenote eq m am l2) (xsdenote eq m am l1);
EQ?.reflexivity eq (select hd am);
CM?.congruence m
(select hd am)
(xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1)
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1))
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2));
CM?.congruence m
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(select hd am)
(xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2))
(select hd am `CM?.mult m` xsdenote eq m am tl);
lemma_xsdenote_aux eq m am hd tl;
EQ?.symmetry eq
(xsdenote eq m am (hd::tl))
(select hd am `CM?.mult m` xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am tl)
(xsdenote eq m am (hd::tl))
end
let rec sort_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom)
: Lemma (requires True)
(ensures xsdenote eq m am xs `CE.EQ?.eq eq` xsdenote eq m am (sort xs))
(decreases (FStar.List.Tot.Base.length xs))
= let open FStar.Algebra.CommMonoid.Equiv in
match xs with
| [] -> EQ?.reflexivity eq (xsdenote eq m am [])
| pivot::q ->
let sort0 : permute = List.Tot.sortWith #int (List.Tot.compare_of_bool (<)) in
let sort_eq (l: list atom) : Lemma
(sort l == sort0 l)
[SMTPat (sort l)]
= sortWith_ext (my_compare_of_bool (<)) (List.Tot.compare_of_bool (<)) l
in
let open FStar.List.Tot.Base in
let f:int -> int -> int = compare_of_bool (<) in
let hi, lo = partition (bool_of_compare f pivot) q in
flatten_correct_aux eq m am (sort lo) (pivot::sort hi);
assert (xsdenote eq m am (sort xs) `EQ?.eq eq`
CM?.mult m (xsdenote eq m am (sort lo))
(xsdenote eq m am (pivot::sort hi)));
lemma_xsdenote_aux eq m am pivot (sort hi);
EQ?.reflexivity eq (xsdenote eq m am (sort lo));
CM?.congruence m
(xsdenote eq m am (sort lo))
(xsdenote eq m am (pivot::sort hi))
(xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` xsdenote eq m am (sort hi));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` xsdenote eq m am (pivot::sort hi))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi))));
CM?.commutativity m
(xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` xsdenote eq m am (sort hi));
CM?.associativity m
(select pivot am)
(xsdenote eq m am (sort hi))
(xsdenote eq m am (sort lo));
EQ?.transitivity eq
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)))
((select pivot am `CM?.mult m` xsdenote eq m am (sort hi)) `CM?.mult m` xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))));
partition_length (bool_of_compare f pivot) q;
sort_correct_aux eq m am hi;
sort_correct_aux eq m am lo;
EQ?.symmetry eq (xsdenote eq m am lo) (xsdenote eq m am (sort lo));
EQ?.symmetry eq (xsdenote eq m am hi) (xsdenote eq m am (sort hi));
CM?.congruence m
(xsdenote eq m am (sort hi))
(xsdenote eq m am (sort lo))
(xsdenote eq m am hi)
(xsdenote eq m am lo);
assert (EQ?.eq eq
(xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo));
EQ?.reflexivity eq (select pivot am);
CM?.congruence m
(select pivot am)
(xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))
(select pivot am)
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo);
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)));
partition_equiv eq m am pivot q;
CM?.congruence m
(select pivot am)
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select pivot am)
(xsdenote eq m am q);
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo))
(select pivot am `CM?.mult m` (xsdenote eq m am q));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am q)));
lemma_xsdenote_aux eq m am pivot q;
EQ?.symmetry eq
(xsdenote eq m am (pivot::q))
(select pivot am `CM?.mult m` (xsdenote eq m am q));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am q))
(xsdenote eq m am xs);
EQ?.symmetry eq (xsdenote eq m am (sort xs)) (xsdenote eq m am xs)
#pop-options
#push-options "--fuel 0 --ifuel 0"
(* Lemmas to be called after a permutation compatible with AC-unification was found *)
let smt_reflexivity (#a:Type) (eq:CE.equiv a) (x y:a)
: Lemma (requires x == y)
(ensures CE.EQ?.eq eq x y)
= CE.EQ?.reflexivity eq x
let identity_left_smt (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x y:a)
: Lemma
(requires x == y)
(ensures CE.EQ?.eq eq x (CE.CM?.mult m (CE.CM?.unit m) y))
= CE.CM?.identity m x;
CE.EQ?.symmetry eq (CE.CM?.mult m (CE.CM?.unit m) x) x
let identity_left (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x:a)
: Lemma (CE.EQ?.eq eq x (CE.CM?.mult m (CE.CM?.unit m) x))
= CE.CM?.identity m x;
CE.EQ?.symmetry eq (CE.CM?.mult m (CE.CM?.unit m) x) x
let identity_right_diff (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x y:a) : Lemma
(requires CE.EQ?.eq eq x y)
(ensures CE.EQ?.eq eq (CE.CM?.mult m x (CE.CM?.unit m)) y)
= CE.right_identity eq m x;
CE.EQ?.transitivity eq (CE.CM?.mult m x (CE.CM?.unit m)) x y
/// Dismiss possible vprops goals that might have been created by lemma application. | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Reflection.V2.Derived.Lemmas",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 0,
"initial_ifuel": 0,
"max_fuel": 0,
"max_ifuel": 0,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Prims.unit -> FStar.Tactics.Effect.Tac Prims.unit | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.unit",
"FStar.Tactics.NamedView.term",
"FStar.Reflection.V2.Derived.is_fvar",
"Prims.bool",
"Steel.Effect.Common.dismiss_slprops",
"FStar.Tactics.V2.Derived.dismiss",
"FStar.Reflection.V2.Formula.formula",
"FStar.Reflection.V2.Formula.term_as_formula'",
"FStar.Tactics.V2.Derived.cur_goal",
"FStar.Stubs.Reflection.Types.typ"
] | [
"recursion"
] | false | true | false | false | false | let rec dismiss_slprops () : Tac unit =
| match term_as_formula' (cur_goal ()) with
| App t _ ->
if is_fvar t (`%squash)
then ()
else
(dismiss ();
dismiss_slprops ())
| _ ->
dismiss ();
dismiss_slprops () | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.trivial_cancel | val trivial_cancel : t: Steel.Effect.Common.atom -> l: Prims.list Steel.Effect.Common.atom
-> Prims.bool * Prims.list Steel.Effect.Common.atom | let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 59,
"end_line": 814,
"start_col": 0,
"start_line": 807
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | t: Steel.Effect.Common.atom -> l: Prims.list Steel.Effect.Common.atom
-> Prims.bool * Prims.list Steel.Effect.Common.atom | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.atom",
"Prims.list",
"FStar.Pervasives.Native.Mktuple2",
"Prims.bool",
"Prims.op_Equality",
"Prims.Cons",
"FStar.Pervasives.Native.tuple2",
"Steel.Effect.Common.trivial_cancel"
] | [
"recursion"
] | false | false | false | true | false | let rec trivial_cancel (t: atom) (l: list atom) =
| match l with
| [] -> false, l
| hd :: tl ->
if hd = t
then true, tl
else
(let b, res = trivial_cancel t tl in
b, hd :: res) | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.visit_tm | val visit_tm (ff: (term -> Tac unit)) (t: term) : Tac unit | val visit_tm (ff: (term -> Tac unit)) (t: term) : Tac unit | let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 30,
"end_line": 719,
"start_col": 0,
"start_line": 655
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
ff: (_: FStar.Tactics.NamedView.term -> FStar.Tactics.Effect.Tac Prims.unit) ->
t: FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac Prims.unit | FStar.Tactics.Effect.Tac | [] | [
"visit_tm",
"visit_br",
"visit_comp"
] | [
"FStar.Tactics.NamedView.term",
"Prims.unit",
"FStar.Stubs.Reflection.Types.fv",
"FStar.Stubs.Reflection.V2.Data.universes",
"FStar.Tactics.NamedView.namedv",
"FStar.Tactics.NamedView.bv",
"FStar.Tactics.NamedView.universe",
"FStar.Stubs.Reflection.V2.Data.vconst",
"Prims.nat",
"FStar.Stubs.Reflection.Types.ctx_uvar_and_subst",
"FStar.Tactics.NamedView.binder",
"FStar.Tactics.NamedView.comp",
"Steel.Effect.Common.visit_comp",
"Steel.Effect.Common.on_sort_binder",
"Steel.Effect.Common.visit_tm",
"FStar.Stubs.Reflection.Types.term",
"FStar.Stubs.Reflection.V2.Data.aqualv",
"FStar.Tactics.NamedView.simple_binder",
"Prims.bool",
"Prims.list",
"FStar.Pervasives.Native.option",
"FStar.Tactics.NamedView.match_returns_ascription",
"FStar.Tactics.NamedView.branch",
"FStar.Tactics.Util.iter",
"Steel.Effect.Common.visit_br",
"FStar.Tactics.NamedView.named_term_view",
"FStar.Tactics.NamedView.inspect"
] | [
"mutual recursion"
] | false | true | false | false | false | let rec visit_tm (ff: (term -> Tac unit)) (t: term) : Tac unit =
| let tv = inspect t in
(match tv with
| Tv_FVar _ | Tv_UInst _ _ | Tv_Var _ | Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ -> visit_tm ff e);
ff t | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.reification | val reification (eq m: term) (ts: list term) (am: amap term) (t: term)
: Tac (exp * list term * amap term) | val reification (eq m: term) (ts: list term) (am: amap term) (t: term)
: Tac (exp * list term * amap term) | let reification (eq: term) (m: term) (ts:list term) (am:amap term) (t:term) :
Tac (exp * list term * amap term) =
let mult = norm_term [iota; zeta; delta] (`CE.CM?.mult (`#m)) in
let unit = norm_term [iota; zeta; delta] (`CE.CM?.unit (`#m)) in
let t = norm_term [iota; zeta] t in
reification_aux ts am mult unit t | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 35,
"end_line": 1648,
"start_col": 0,
"start_line": 1643
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am
/// Iterates over all terms in [l2] to prepare them for unification with SMT rewriting
let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2
/// Recursively calls equivalent_lists_once.
/// Stops when we're done with unification, or when we didn't make any progress
/// If we didn't make any progress, we have too many candidates for some terms.
/// Accumulates rewritings of l1 and l2 in l1_del and l2_del, with the invariant
/// that the two lists are unifiable at any point
/// The boolean indicates if there is a leftover empty frame
let rec equivalent_lists' (n:nat) (use_smt:bool) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, []
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Try to make progress by matching non-uvars of l2 with candidates in l1
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Should always be smaller or equal to n
// If it is equal, no progress was made.
if use_smt then
// SMT fallback is allowed
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
/// Checks if term for atom t unifies with fall uvars in l
let rec unifies_with_all_uvars (t:term) (l:list atom) (am:amap term) : Tac bool =
match l with
| [] -> true
| hd::tl ->
if unifies_with_all_uvars t tl am then (
// Unified with tail, try this term
let hd_t = select hd am in
if is_uvar hd_t then (
// The head term is a uvar, try unifying
try if unify t hd_t then raise Success else raise Failed
with | Success -> true | _ -> false
) else true // The uvar is not a head term, we do not need to try it
) else false
/// Puts all terms in l1 that cannot unify with the uvars in l2 at the top:
/// They need to be solved first
let rec most_restricted_at_top (l1 l2:list atom) (am:amap term) : Tac (list atom) =
match l1 with
| [] -> []
| hd::tl ->
if unifies_with_all_uvars (select hd am) l2 am then (most_restricted_at_top tl l2 am) `List.Tot.append` [hd]
else hd::(most_restricted_at_top tl l2 am)
/// Core AC-unification tactic.
/// First remove all trivially equal terms, then try to decide equivalence.
/// Assumes that l1 does not contain any vprop uvar.
/// If it succeeds, returns permutations of l1, l2, and a boolean indicating
/// if l2 has a trailing empty frame to be unified
let equivalent_lists (use_smt:bool) (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term)
= let l1, l2, l1_del, l2_del = trivial_cancels l1 l2 am in
let l1 = most_restricted_at_top l1 l2 am in
let n = List.Tot.length l1 in
let l1_del, l2_del, emp_frame, uvar_terms = equivalent_lists' n use_smt l1 l2 l1_del l2_del am in
l1_del, l2_del, emp_frame, uvar_terms
(* Helpers to relate the actual terms to their representation as a list of atoms *)
open FStar.Reflection.V2.Derived.Lemmas
let rec list_to_string (l:list term) : Tac string =
match l with
| [] -> "end"
| hd::tl -> term_to_string hd ^ " " ^ list_to_string tl
let rec mdenote_gen (#a:Type u#aa) (unit:a) (mult:a -> a -> a) (am:amap a) (e:exp) : a =
match e with
| Unit -> unit
| Atom x -> select x am
| Mult e1 e2 -> mult (mdenote_gen unit mult am e1) (mdenote_gen unit mult am e2)
let rec xsdenote_gen (#a:Type) (unit:a) (mult:a -> a -> a) (am:amap a) (xs:list atom) : a =
match xs with
| [] -> unit
| [x] -> select x am
| x::xs' -> mult (select x am) (xsdenote_gen unit mult am xs')
unfold
let mdenote (#a:Type u#aa) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp) : a =
let open FStar.Algebra.CommMonoid.Equiv in
mdenote_gen (CM?.unit m) (CM?.mult m) am e
unfold
let xsdenote (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom) : a =
let open FStar.Algebra.CommMonoid.Equiv in
xsdenote_gen (CM?.unit m) (CM?.mult m) am xs
// We reimplement List.Tot.Base.append because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_append (#t: Type) (l1 l2: list t) : Pure (list t)
(requires True)
(ensures (fun res -> res == l1 `List.Tot.append` l2))
(decreases l1)
= match l1 with
| [] -> l2
| a :: q -> a :: my_append q l2
let rec flatten (e:exp) : list atom =
match e with
| Unit -> []
| Atom x -> [x]
| Mult e1 e2 -> flatten e1 `my_append` flatten e2
let rec flatten_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs1 xs2:list atom)
: Lemma (xsdenote eq m am (xs1 `my_append` xs2) `CE.EQ?.eq eq` CE.CM?.mult m (xsdenote eq m am xs1)
(xsdenote eq m am xs2)) =
let open FStar.Algebra.CommMonoid.Equiv in
match xs1 with
| [] ->
CM?.identity m (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.unit m) (xsdenote eq m am xs2)) (xsdenote eq m am xs2)
| [x] -> (
if (Nil? xs2)
then (right_identity eq m (select x am);
EQ?.symmetry eq (CM?.mult m (select x am) (CM?.unit m)) (select x am))
else EQ?.reflexivity eq (CM?.mult m (xsdenote eq m am [x]) (xsdenote eq m am xs2)))
| x::xs1' ->
flatten_correct_aux eq m am xs1' xs2;
EQ?.reflexivity eq (select x am);
CM?.congruence m (select x am) (xsdenote eq m am (xs1' `my_append` xs2))
(select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2));
CM?.associativity m (select x am) (xsdenote eq m am xs1') (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)));
EQ?.transitivity eq (CM?.mult m (select x am) (xsdenote eq m am (xs1' `my_append` xs2)))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)))
(CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
let rec flatten_correct (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp)
: Lemma (mdenote eq m am e `CE.EQ?.eq eq` xsdenote eq m am (flatten e)) =
let open FStar.Algebra.CommMonoid.Equiv in
match e with
| Unit -> EQ?.reflexivity eq (CM?.unit m)
| Atom x -> EQ?.reflexivity eq (select x am)
| Mult e1 e2 ->
flatten_correct_aux eq m am (flatten e1) (flatten e2);
EQ?.symmetry eq (xsdenote eq m am (flatten e1 `my_append` flatten e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)));
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CM?.congruence m (mdenote eq m am e1) (mdenote eq m am e2)
(xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2));
EQ?.transitivity eq (CM?.mult m (mdenote eq m am e1) (mdenote eq m am e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)))
(xsdenote eq m am (flatten e1 `my_append` flatten e2))
let monoid_reflect (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e1 e2:exp)
(_ : squash (xsdenote eq m am (flatten e1) `CE.EQ?.eq eq` xsdenote eq m am (flatten e2)))
: squash (mdenote eq m am e1 `CE.EQ?.eq eq` mdenote eq m am e2) =
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CE.EQ?.symmetry eq (mdenote eq m am e2) (xsdenote eq m am (flatten e2));
CE.EQ?.transitivity eq
(xsdenote eq m am (flatten e1))
(xsdenote eq m am (flatten e2))
(mdenote eq m am e2);
CE.EQ?.transitivity eq
(mdenote eq m am e1)
(xsdenote eq m am (flatten e1))
(mdenote eq m am e2)
// Here we sort the variable numbers
// We reimplement List.Tot.Base.sortWith because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_partition (#a: Type) (f: (a -> Tot bool)) (l: list a)
: Pure (list a & list a)
(requires True)
(ensures (fun res -> res == List.Tot.partition f l))
= match l with
| [] -> [], []
| hd::tl ->
let l1, l2 = my_partition f tl in
if f hd
then hd::l1, l2
else l1, hd::l2
let rec partition_ext (#a: Type) (f1 f2: (a -> Tot bool)) (l: list a)
: Lemma
(requires (forall x . f1 x == f2 x))
(ensures (List.Tot.partition f1 l == List.Tot.partition f2 l))
= match l with
| [] -> ()
| hd::tl -> partition_ext f1 f2 tl
let my_bool_of_compare (#a: Type) (f: a -> a -> Tot int) (x: a) (y: a) : Tot bool
= f x y < 0
let rec my_sortWith (#a: Type) (f: (a -> a -> Tot int)) (l:list a)
: Pure (list a)
(requires True)
(ensures (fun res -> res == List.Tot.sortWith f l))
(decreases (List.Tot.length l))
= match l with
| [] -> []
| pivot::tl ->
let hi, lo = my_partition (my_bool_of_compare f pivot) tl in
partition_ext (my_bool_of_compare f pivot) (List.Tot.bool_of_compare f pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f pivot) tl;
my_append (my_sortWith f lo) (pivot::my_sortWith f hi)
let rec sortWith_ext (#a: Type) (f1 f2: (a -> a -> Tot int)) (l: list a)
: Lemma
(requires (forall x y . f1 x y == f2 x y))
(ensures (List.Tot.sortWith f1 l == List.Tot.sortWith f2 l))
(decreases (List.Tot.length l))
= match l with
| [] -> ()
| pivot::tl ->
partition_ext (List.Tot.bool_of_compare f1 pivot) (List.Tot.bool_of_compare f2 pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f1 pivot) tl;
let hi, lo = List.Tot.partition (List.Tot.bool_of_compare f1 pivot) tl in
sortWith_ext f1 f2 lo;
sortWith_ext f1 f2 hi
let permute = list atom -> list atom
let my_compare_of_bool (#a:eqtype) (rel: a -> a -> Tot bool) (x: a) (y: a) : Tot int
= if x `rel` y then -1
else if x = y then 0
else 1
let sort : permute = my_sortWith #int (my_compare_of_bool (<))
#push-options "--fuel 1 --ifuel 1"
let lemma_xsdenote_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (hd:atom) (tl:list atom)
: Lemma (xsdenote eq m am (hd::tl) `CE.EQ?.eq eq`
(CE.CM?.mult m (select hd am) (xsdenote eq m am tl)))
= let open FStar.Algebra.CommMonoid.Equiv in
match tl with
| [] ->
assert (xsdenote eq m am (hd::tl) == select hd am);
CM?.identity m (select hd am);
EQ?.symmetry eq (CM?.unit m `CM?.mult m` select hd am) (select hd am);
CM?.commutativity m (CM?.unit m) (select hd am);
EQ?.transitivity eq
(xsdenote eq m am (hd::tl))
(CM?.unit m `CM?.mult m` select hd am)
(CM?.mult m (select hd am) (xsdenote eq m am tl))
| _ -> EQ?.reflexivity eq (xsdenote eq m am (hd::tl))
let rec partition_equiv (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (pivot:atom) (q:list atom)
: Lemma
(let open FStar.List.Tot.Base in
let hi, lo = partition (bool_of_compare (compare_of_bool (<)) pivot) q in
CE.EQ?.eq eq
(xsdenote eq m am hi `CE.CM?.mult m` xsdenote eq m am lo)
(xsdenote eq m am q))
= let open FStar.Algebra.CommMonoid.Equiv in
let open FStar.List.Tot.Base in
let f = bool_of_compare (compare_of_bool (<)) pivot in
let hi, lo = partition f q in
match q with
| [] -> CM?.identity m (xsdenote eq m am hi)
| hd::tl ->
let l1, l2 = partition f tl in
partition_equiv eq m am pivot tl;
assert (EQ?.eq eq
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(xsdenote eq m am tl));
EQ?.reflexivity eq (xsdenote eq m am l1);
EQ?.reflexivity eq (xsdenote eq m am l2);
EQ?.reflexivity eq (xsdenote eq m am hi);
EQ?.reflexivity eq (xsdenote eq m am lo);
if f hd then begin
assert (hi == hd::l1 /\ lo == l2);
lemma_xsdenote_aux eq m am hd l1;
CM?.congruence m
(xsdenote eq m am hi)
(xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am l1)
(xsdenote eq m am l2);
CM?.associativity m
(select hd am)
(xsdenote eq m am l1)
(xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
((select hd am `CM?.mult m` xsdenote eq m am l1) `CM?.mult m` xsdenote eq m am l2)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2));
EQ?.reflexivity eq (select hd am);
CM?.congruence m
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(select hd am)
(xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2))
(select hd am `CM?.mult m` xsdenote eq m am tl);
lemma_xsdenote_aux eq m am hd tl;
EQ?.symmetry eq
(xsdenote eq m am (hd::tl))
(select hd am `CM?.mult m` xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am tl)
(xsdenote eq m am (hd::tl))
end else begin
assert (hi == l1 /\ lo == hd::l2);
lemma_xsdenote_aux eq m am hd l2;
CM?.congruence m
(xsdenote eq m am hi)
(xsdenote eq m am lo)
(xsdenote eq m am l1)
(select hd am `CM?.mult m` xsdenote eq m am l2);
CM?.commutativity m
(xsdenote eq m am l1)
(select hd am `CM?.mult m` xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(xsdenote eq m am l1 `CM?.mult m` (select hd am `CM?.mult m` xsdenote eq m am l2))
((select hd am `CM?.mult m` xsdenote eq m am l2) `CM?.mult m` xsdenote eq m am l1);
CM?.associativity m
(select hd am)
(xsdenote eq m am l2)
(xsdenote eq m am l1);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
((select hd am `CM?.mult m` xsdenote eq m am l2) `CM?.mult m` xsdenote eq m am l1)
(select hd am `CM?.mult m` (xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1));
CM?.commutativity m (xsdenote eq m am l2) (xsdenote eq m am l1);
EQ?.reflexivity eq (select hd am);
CM?.congruence m
(select hd am)
(xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1)
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1))
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2));
CM?.congruence m
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(select hd am)
(xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2))
(select hd am `CM?.mult m` xsdenote eq m am tl);
lemma_xsdenote_aux eq m am hd tl;
EQ?.symmetry eq
(xsdenote eq m am (hd::tl))
(select hd am `CM?.mult m` xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am tl)
(xsdenote eq m am (hd::tl))
end
let rec sort_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom)
: Lemma (requires True)
(ensures xsdenote eq m am xs `CE.EQ?.eq eq` xsdenote eq m am (sort xs))
(decreases (FStar.List.Tot.Base.length xs))
= let open FStar.Algebra.CommMonoid.Equiv in
match xs with
| [] -> EQ?.reflexivity eq (xsdenote eq m am [])
| pivot::q ->
let sort0 : permute = List.Tot.sortWith #int (List.Tot.compare_of_bool (<)) in
let sort_eq (l: list atom) : Lemma
(sort l == sort0 l)
[SMTPat (sort l)]
= sortWith_ext (my_compare_of_bool (<)) (List.Tot.compare_of_bool (<)) l
in
let open FStar.List.Tot.Base in
let f:int -> int -> int = compare_of_bool (<) in
let hi, lo = partition (bool_of_compare f pivot) q in
flatten_correct_aux eq m am (sort lo) (pivot::sort hi);
assert (xsdenote eq m am (sort xs) `EQ?.eq eq`
CM?.mult m (xsdenote eq m am (sort lo))
(xsdenote eq m am (pivot::sort hi)));
lemma_xsdenote_aux eq m am pivot (sort hi);
EQ?.reflexivity eq (xsdenote eq m am (sort lo));
CM?.congruence m
(xsdenote eq m am (sort lo))
(xsdenote eq m am (pivot::sort hi))
(xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` xsdenote eq m am (sort hi));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` xsdenote eq m am (pivot::sort hi))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi))));
CM?.commutativity m
(xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` xsdenote eq m am (sort hi));
CM?.associativity m
(select pivot am)
(xsdenote eq m am (sort hi))
(xsdenote eq m am (sort lo));
EQ?.transitivity eq
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)))
((select pivot am `CM?.mult m` xsdenote eq m am (sort hi)) `CM?.mult m` xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))));
partition_length (bool_of_compare f pivot) q;
sort_correct_aux eq m am hi;
sort_correct_aux eq m am lo;
EQ?.symmetry eq (xsdenote eq m am lo) (xsdenote eq m am (sort lo));
EQ?.symmetry eq (xsdenote eq m am hi) (xsdenote eq m am (sort hi));
CM?.congruence m
(xsdenote eq m am (sort hi))
(xsdenote eq m am (sort lo))
(xsdenote eq m am hi)
(xsdenote eq m am lo);
assert (EQ?.eq eq
(xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo));
EQ?.reflexivity eq (select pivot am);
CM?.congruence m
(select pivot am)
(xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))
(select pivot am)
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo);
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)));
partition_equiv eq m am pivot q;
CM?.congruence m
(select pivot am)
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select pivot am)
(xsdenote eq m am q);
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo))
(select pivot am `CM?.mult m` (xsdenote eq m am q));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am q)));
lemma_xsdenote_aux eq m am pivot q;
EQ?.symmetry eq
(xsdenote eq m am (pivot::q))
(select pivot am `CM?.mult m` (xsdenote eq m am q));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am q))
(xsdenote eq m am xs);
EQ?.symmetry eq (xsdenote eq m am (sort xs)) (xsdenote eq m am xs)
#pop-options
#push-options "--fuel 0 --ifuel 0"
(* Lemmas to be called after a permutation compatible with AC-unification was found *)
let smt_reflexivity (#a:Type) (eq:CE.equiv a) (x y:a)
: Lemma (requires x == y)
(ensures CE.EQ?.eq eq x y)
= CE.EQ?.reflexivity eq x
let identity_left_smt (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x y:a)
: Lemma
(requires x == y)
(ensures CE.EQ?.eq eq x (CE.CM?.mult m (CE.CM?.unit m) y))
= CE.CM?.identity m x;
CE.EQ?.symmetry eq (CE.CM?.mult m (CE.CM?.unit m) x) x
let identity_left (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x:a)
: Lemma (CE.EQ?.eq eq x (CE.CM?.mult m (CE.CM?.unit m) x))
= CE.CM?.identity m x;
CE.EQ?.symmetry eq (CE.CM?.mult m (CE.CM?.unit m) x) x
let identity_right_diff (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x y:a) : Lemma
(requires CE.EQ?.eq eq x y)
(ensures CE.EQ?.eq eq (CE.CM?.mult m x (CE.CM?.unit m)) y)
= CE.right_identity eq m x;
CE.EQ?.transitivity eq (CE.CM?.mult m x (CE.CM?.unit m)) x y
/// Dismiss possible vprops goals that might have been created by lemma application.
/// These vprops will be instantiated at a later stage; else, Meta-F* will raise an error
let rec dismiss_slprops () : Tac unit =
match term_as_formula' (cur_goal ()) with
| App t _ -> if is_fvar t (`%squash) then () else (dismiss(); dismiss_slprops ())
| _ -> dismiss(); dismiss_slprops ()
/// Recursively removing trailing empty assertions
let rec n_identity_left (n:int) (eq m:term) : Tac unit
= if n = 0 then (
apply_lemma (`(CE.EQ?.reflexivity (`#eq)));
// Cleaning up, in case a uvar has been generated here. It'll be solved later
set_goals [])
else (
apply_lemma (`identity_right_diff (`#eq) (`#m));
// Drop the slprops generated, they will be solved later
dismiss_slprops ();
n_identity_left (n-1) eq m
)
/// Helper lemma: If two vprops (as represented by lists of atoms) are equivalent, then their canonical forms
/// (corresponding to applying the sort function on atoms) are equivalent
let equivalent_sorted (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (l1 l2 l1' l2':list atom)
: Lemma (requires
sort l1 == sort l1' /\
sort l2 == sort l2' /\
xsdenote eq m am l1 `CE.EQ?.eq eq` xsdenote eq m am l2)
(ensures xsdenote eq m am l1' `CE.EQ?.eq eq` xsdenote eq m am l2')
= let open FStar.Algebra.CommMonoid.Equiv in
sort_correct_aux eq m am l1';
sort_correct_aux eq m am l1;
EQ?.symmetry eq (xsdenote eq m am l1) (xsdenote eq m am (sort l1));
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am (sort l1'))
(xsdenote eq m am l1);
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am l1)
(xsdenote eq m am l2);
sort_correct_aux eq m am l2;
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am l2)
(xsdenote eq m am (sort l2));
sort_correct_aux eq m am l2';
EQ?.symmetry eq (xsdenote eq m am l2') (xsdenote eq m am (sort l2'));
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am (sort l2))
(xsdenote eq m am l2')
#pop-options
/// Finds the position of first occurrence of x in xs.
/// This is now specialized to terms and their funny term_eq_old.
let rec where_aux (n:nat) (x:term) (xs:list term) :
Tac (option nat) (decreases xs) =
match xs with
| [] -> None
| x'::xs' -> if term_eq_old x x' then Some n else where_aux (n+1) x xs'
let where = where_aux 0
let fatom (t:term) (ts:list term) (am:amap term) : Tac (exp * list term * amap term) =
match where t ts with
| Some v -> (Atom v, ts, am)
| None ->
let vfresh = List.Tot.Base.length ts in
let t = norm_term [iota; zeta] t in
(Atom vfresh, ts `List.Tot.append` [t], update vfresh t am)
/// Transforming a term into the corresponding list of atoms
/// If the atomic terms were already present in the map [am], then
/// they correspond to the same atoms
/// This expects that mult, unit, and t have already been normalized
let rec reification_aux (ts:list term) (am:amap term)
(mult unit t : term) : Tac (exp * list term * amap term) =
let hd, tl = collect_app_ref t in
match inspect_unascribe hd, List.Tot.Base.list_unref tl with
| Tv_FVar fv, [(t1, Q_Explicit) ; (t2, Q_Explicit)] ->
if term_eq_old (pack (Tv_FVar fv)) mult
then (let (e1, ts, am) = reification_aux ts am mult unit t1 in
let (e2, ts, am) = reification_aux ts am mult unit t2 in
(Mult e1 e2, ts, am))
else fatom t ts am
| _, _ ->
if term_eq_old t unit
then (Unit, ts, am)
else fatom t ts am | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Reflection.V2.Derived.Lemmas",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
eq: FStar.Tactics.NamedView.term ->
m: FStar.Tactics.NamedView.term ->
ts: Prims.list FStar.Tactics.NamedView.term ->
am: Steel.Effect.Common.amap FStar.Tactics.NamedView.term ->
t: FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac
((Steel.Effect.Common.exp * Prims.list FStar.Tactics.NamedView.term) *
Steel.Effect.Common.amap FStar.Tactics.NamedView.term) | FStar.Tactics.Effect.Tac | [] | [] | [
"FStar.Tactics.NamedView.term",
"Prims.list",
"Steel.Effect.Common.amap",
"Steel.Effect.Common.reification_aux",
"FStar.Pervasives.Native.tuple3",
"Steel.Effect.Common.exp",
"FStar.Tactics.V2.Derived.norm_term",
"Prims.Cons",
"FStar.Pervasives.norm_step",
"FStar.Pervasives.iota",
"FStar.Pervasives.zeta",
"Prims.Nil",
"FStar.Pervasives.delta"
] | [] | false | true | false | false | false | let reification (eq m: term) (ts: list term) (am: amap term) (t: term)
: Tac (exp * list term * amap term) =
| let mult = norm_term [iota; zeta; delta] (`CE.CM?.mult (`#m)) in
let unit = norm_term [iota; zeta; delta] (`CE.CM?.unit (`#m)) in
let t = norm_term [iota; zeta] t in
reification_aux ts am mult unit t | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.convert_am | val convert_am (am: amap term) : term | val convert_am (am: amap term) : term | let convert_am (am : amap term) : term =
let (map, def) = am in
(* let def = norm_term [delta] def in *)
`( (`#(convert_map map), `#def) ) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 35,
"end_line": 1666,
"start_col": 0,
"start_line": 1663
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am
/// Iterates over all terms in [l2] to prepare them for unification with SMT rewriting
let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2
/// Recursively calls equivalent_lists_once.
/// Stops when we're done with unification, or when we didn't make any progress
/// If we didn't make any progress, we have too many candidates for some terms.
/// Accumulates rewritings of l1 and l2 in l1_del and l2_del, with the invariant
/// that the two lists are unifiable at any point
/// The boolean indicates if there is a leftover empty frame
let rec equivalent_lists' (n:nat) (use_smt:bool) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, []
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Try to make progress by matching non-uvars of l2 with candidates in l1
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Should always be smaller or equal to n
// If it is equal, no progress was made.
if use_smt then
// SMT fallback is allowed
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
/// Checks if term for atom t unifies with fall uvars in l
let rec unifies_with_all_uvars (t:term) (l:list atom) (am:amap term) : Tac bool =
match l with
| [] -> true
| hd::tl ->
if unifies_with_all_uvars t tl am then (
// Unified with tail, try this term
let hd_t = select hd am in
if is_uvar hd_t then (
// The head term is a uvar, try unifying
try if unify t hd_t then raise Success else raise Failed
with | Success -> true | _ -> false
) else true // The uvar is not a head term, we do not need to try it
) else false
/// Puts all terms in l1 that cannot unify with the uvars in l2 at the top:
/// They need to be solved first
let rec most_restricted_at_top (l1 l2:list atom) (am:amap term) : Tac (list atom) =
match l1 with
| [] -> []
| hd::tl ->
if unifies_with_all_uvars (select hd am) l2 am then (most_restricted_at_top tl l2 am) `List.Tot.append` [hd]
else hd::(most_restricted_at_top tl l2 am)
/// Core AC-unification tactic.
/// First remove all trivially equal terms, then try to decide equivalence.
/// Assumes that l1 does not contain any vprop uvar.
/// If it succeeds, returns permutations of l1, l2, and a boolean indicating
/// if l2 has a trailing empty frame to be unified
let equivalent_lists (use_smt:bool) (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term)
= let l1, l2, l1_del, l2_del = trivial_cancels l1 l2 am in
let l1 = most_restricted_at_top l1 l2 am in
let n = List.Tot.length l1 in
let l1_del, l2_del, emp_frame, uvar_terms = equivalent_lists' n use_smt l1 l2 l1_del l2_del am in
l1_del, l2_del, emp_frame, uvar_terms
(* Helpers to relate the actual terms to their representation as a list of atoms *)
open FStar.Reflection.V2.Derived.Lemmas
let rec list_to_string (l:list term) : Tac string =
match l with
| [] -> "end"
| hd::tl -> term_to_string hd ^ " " ^ list_to_string tl
let rec mdenote_gen (#a:Type u#aa) (unit:a) (mult:a -> a -> a) (am:amap a) (e:exp) : a =
match e with
| Unit -> unit
| Atom x -> select x am
| Mult e1 e2 -> mult (mdenote_gen unit mult am e1) (mdenote_gen unit mult am e2)
let rec xsdenote_gen (#a:Type) (unit:a) (mult:a -> a -> a) (am:amap a) (xs:list atom) : a =
match xs with
| [] -> unit
| [x] -> select x am
| x::xs' -> mult (select x am) (xsdenote_gen unit mult am xs')
unfold
let mdenote (#a:Type u#aa) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp) : a =
let open FStar.Algebra.CommMonoid.Equiv in
mdenote_gen (CM?.unit m) (CM?.mult m) am e
unfold
let xsdenote (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom) : a =
let open FStar.Algebra.CommMonoid.Equiv in
xsdenote_gen (CM?.unit m) (CM?.mult m) am xs
// We reimplement List.Tot.Base.append because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_append (#t: Type) (l1 l2: list t) : Pure (list t)
(requires True)
(ensures (fun res -> res == l1 `List.Tot.append` l2))
(decreases l1)
= match l1 with
| [] -> l2
| a :: q -> a :: my_append q l2
let rec flatten (e:exp) : list atom =
match e with
| Unit -> []
| Atom x -> [x]
| Mult e1 e2 -> flatten e1 `my_append` flatten e2
let rec flatten_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs1 xs2:list atom)
: Lemma (xsdenote eq m am (xs1 `my_append` xs2) `CE.EQ?.eq eq` CE.CM?.mult m (xsdenote eq m am xs1)
(xsdenote eq m am xs2)) =
let open FStar.Algebra.CommMonoid.Equiv in
match xs1 with
| [] ->
CM?.identity m (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.unit m) (xsdenote eq m am xs2)) (xsdenote eq m am xs2)
| [x] -> (
if (Nil? xs2)
then (right_identity eq m (select x am);
EQ?.symmetry eq (CM?.mult m (select x am) (CM?.unit m)) (select x am))
else EQ?.reflexivity eq (CM?.mult m (xsdenote eq m am [x]) (xsdenote eq m am xs2)))
| x::xs1' ->
flatten_correct_aux eq m am xs1' xs2;
EQ?.reflexivity eq (select x am);
CM?.congruence m (select x am) (xsdenote eq m am (xs1' `my_append` xs2))
(select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2));
CM?.associativity m (select x am) (xsdenote eq m am xs1') (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)));
EQ?.transitivity eq (CM?.mult m (select x am) (xsdenote eq m am (xs1' `my_append` xs2)))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)))
(CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
let rec flatten_correct (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp)
: Lemma (mdenote eq m am e `CE.EQ?.eq eq` xsdenote eq m am (flatten e)) =
let open FStar.Algebra.CommMonoid.Equiv in
match e with
| Unit -> EQ?.reflexivity eq (CM?.unit m)
| Atom x -> EQ?.reflexivity eq (select x am)
| Mult e1 e2 ->
flatten_correct_aux eq m am (flatten e1) (flatten e2);
EQ?.symmetry eq (xsdenote eq m am (flatten e1 `my_append` flatten e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)));
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CM?.congruence m (mdenote eq m am e1) (mdenote eq m am e2)
(xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2));
EQ?.transitivity eq (CM?.mult m (mdenote eq m am e1) (mdenote eq m am e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)))
(xsdenote eq m am (flatten e1 `my_append` flatten e2))
let monoid_reflect (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e1 e2:exp)
(_ : squash (xsdenote eq m am (flatten e1) `CE.EQ?.eq eq` xsdenote eq m am (flatten e2)))
: squash (mdenote eq m am e1 `CE.EQ?.eq eq` mdenote eq m am e2) =
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CE.EQ?.symmetry eq (mdenote eq m am e2) (xsdenote eq m am (flatten e2));
CE.EQ?.transitivity eq
(xsdenote eq m am (flatten e1))
(xsdenote eq m am (flatten e2))
(mdenote eq m am e2);
CE.EQ?.transitivity eq
(mdenote eq m am e1)
(xsdenote eq m am (flatten e1))
(mdenote eq m am e2)
// Here we sort the variable numbers
// We reimplement List.Tot.Base.sortWith because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_partition (#a: Type) (f: (a -> Tot bool)) (l: list a)
: Pure (list a & list a)
(requires True)
(ensures (fun res -> res == List.Tot.partition f l))
= match l with
| [] -> [], []
| hd::tl ->
let l1, l2 = my_partition f tl in
if f hd
then hd::l1, l2
else l1, hd::l2
let rec partition_ext (#a: Type) (f1 f2: (a -> Tot bool)) (l: list a)
: Lemma
(requires (forall x . f1 x == f2 x))
(ensures (List.Tot.partition f1 l == List.Tot.partition f2 l))
= match l with
| [] -> ()
| hd::tl -> partition_ext f1 f2 tl
let my_bool_of_compare (#a: Type) (f: a -> a -> Tot int) (x: a) (y: a) : Tot bool
= f x y < 0
let rec my_sortWith (#a: Type) (f: (a -> a -> Tot int)) (l:list a)
: Pure (list a)
(requires True)
(ensures (fun res -> res == List.Tot.sortWith f l))
(decreases (List.Tot.length l))
= match l with
| [] -> []
| pivot::tl ->
let hi, lo = my_partition (my_bool_of_compare f pivot) tl in
partition_ext (my_bool_of_compare f pivot) (List.Tot.bool_of_compare f pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f pivot) tl;
my_append (my_sortWith f lo) (pivot::my_sortWith f hi)
let rec sortWith_ext (#a: Type) (f1 f2: (a -> a -> Tot int)) (l: list a)
: Lemma
(requires (forall x y . f1 x y == f2 x y))
(ensures (List.Tot.sortWith f1 l == List.Tot.sortWith f2 l))
(decreases (List.Tot.length l))
= match l with
| [] -> ()
| pivot::tl ->
partition_ext (List.Tot.bool_of_compare f1 pivot) (List.Tot.bool_of_compare f2 pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f1 pivot) tl;
let hi, lo = List.Tot.partition (List.Tot.bool_of_compare f1 pivot) tl in
sortWith_ext f1 f2 lo;
sortWith_ext f1 f2 hi
let permute = list atom -> list atom
let my_compare_of_bool (#a:eqtype) (rel: a -> a -> Tot bool) (x: a) (y: a) : Tot int
= if x `rel` y then -1
else if x = y then 0
else 1
let sort : permute = my_sortWith #int (my_compare_of_bool (<))
#push-options "--fuel 1 --ifuel 1"
let lemma_xsdenote_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (hd:atom) (tl:list atom)
: Lemma (xsdenote eq m am (hd::tl) `CE.EQ?.eq eq`
(CE.CM?.mult m (select hd am) (xsdenote eq m am tl)))
= let open FStar.Algebra.CommMonoid.Equiv in
match tl with
| [] ->
assert (xsdenote eq m am (hd::tl) == select hd am);
CM?.identity m (select hd am);
EQ?.symmetry eq (CM?.unit m `CM?.mult m` select hd am) (select hd am);
CM?.commutativity m (CM?.unit m) (select hd am);
EQ?.transitivity eq
(xsdenote eq m am (hd::tl))
(CM?.unit m `CM?.mult m` select hd am)
(CM?.mult m (select hd am) (xsdenote eq m am tl))
| _ -> EQ?.reflexivity eq (xsdenote eq m am (hd::tl))
let rec partition_equiv (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (pivot:atom) (q:list atom)
: Lemma
(let open FStar.List.Tot.Base in
let hi, lo = partition (bool_of_compare (compare_of_bool (<)) pivot) q in
CE.EQ?.eq eq
(xsdenote eq m am hi `CE.CM?.mult m` xsdenote eq m am lo)
(xsdenote eq m am q))
= let open FStar.Algebra.CommMonoid.Equiv in
let open FStar.List.Tot.Base in
let f = bool_of_compare (compare_of_bool (<)) pivot in
let hi, lo = partition f q in
match q with
| [] -> CM?.identity m (xsdenote eq m am hi)
| hd::tl ->
let l1, l2 = partition f tl in
partition_equiv eq m am pivot tl;
assert (EQ?.eq eq
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(xsdenote eq m am tl));
EQ?.reflexivity eq (xsdenote eq m am l1);
EQ?.reflexivity eq (xsdenote eq m am l2);
EQ?.reflexivity eq (xsdenote eq m am hi);
EQ?.reflexivity eq (xsdenote eq m am lo);
if f hd then begin
assert (hi == hd::l1 /\ lo == l2);
lemma_xsdenote_aux eq m am hd l1;
CM?.congruence m
(xsdenote eq m am hi)
(xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am l1)
(xsdenote eq m am l2);
CM?.associativity m
(select hd am)
(xsdenote eq m am l1)
(xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
((select hd am `CM?.mult m` xsdenote eq m am l1) `CM?.mult m` xsdenote eq m am l2)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2));
EQ?.reflexivity eq (select hd am);
CM?.congruence m
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(select hd am)
(xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2))
(select hd am `CM?.mult m` xsdenote eq m am tl);
lemma_xsdenote_aux eq m am hd tl;
EQ?.symmetry eq
(xsdenote eq m am (hd::tl))
(select hd am `CM?.mult m` xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am tl)
(xsdenote eq m am (hd::tl))
end else begin
assert (hi == l1 /\ lo == hd::l2);
lemma_xsdenote_aux eq m am hd l2;
CM?.congruence m
(xsdenote eq m am hi)
(xsdenote eq m am lo)
(xsdenote eq m am l1)
(select hd am `CM?.mult m` xsdenote eq m am l2);
CM?.commutativity m
(xsdenote eq m am l1)
(select hd am `CM?.mult m` xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(xsdenote eq m am l1 `CM?.mult m` (select hd am `CM?.mult m` xsdenote eq m am l2))
((select hd am `CM?.mult m` xsdenote eq m am l2) `CM?.mult m` xsdenote eq m am l1);
CM?.associativity m
(select hd am)
(xsdenote eq m am l2)
(xsdenote eq m am l1);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
((select hd am `CM?.mult m` xsdenote eq m am l2) `CM?.mult m` xsdenote eq m am l1)
(select hd am `CM?.mult m` (xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1));
CM?.commutativity m (xsdenote eq m am l2) (xsdenote eq m am l1);
EQ?.reflexivity eq (select hd am);
CM?.congruence m
(select hd am)
(xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1)
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1))
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2));
CM?.congruence m
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(select hd am)
(xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2))
(select hd am `CM?.mult m` xsdenote eq m am tl);
lemma_xsdenote_aux eq m am hd tl;
EQ?.symmetry eq
(xsdenote eq m am (hd::tl))
(select hd am `CM?.mult m` xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am tl)
(xsdenote eq m am (hd::tl))
end
let rec sort_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom)
: Lemma (requires True)
(ensures xsdenote eq m am xs `CE.EQ?.eq eq` xsdenote eq m am (sort xs))
(decreases (FStar.List.Tot.Base.length xs))
= let open FStar.Algebra.CommMonoid.Equiv in
match xs with
| [] -> EQ?.reflexivity eq (xsdenote eq m am [])
| pivot::q ->
let sort0 : permute = List.Tot.sortWith #int (List.Tot.compare_of_bool (<)) in
let sort_eq (l: list atom) : Lemma
(sort l == sort0 l)
[SMTPat (sort l)]
= sortWith_ext (my_compare_of_bool (<)) (List.Tot.compare_of_bool (<)) l
in
let open FStar.List.Tot.Base in
let f:int -> int -> int = compare_of_bool (<) in
let hi, lo = partition (bool_of_compare f pivot) q in
flatten_correct_aux eq m am (sort lo) (pivot::sort hi);
assert (xsdenote eq m am (sort xs) `EQ?.eq eq`
CM?.mult m (xsdenote eq m am (sort lo))
(xsdenote eq m am (pivot::sort hi)));
lemma_xsdenote_aux eq m am pivot (sort hi);
EQ?.reflexivity eq (xsdenote eq m am (sort lo));
CM?.congruence m
(xsdenote eq m am (sort lo))
(xsdenote eq m am (pivot::sort hi))
(xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` xsdenote eq m am (sort hi));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` xsdenote eq m am (pivot::sort hi))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi))));
CM?.commutativity m
(xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` xsdenote eq m am (sort hi));
CM?.associativity m
(select pivot am)
(xsdenote eq m am (sort hi))
(xsdenote eq m am (sort lo));
EQ?.transitivity eq
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)))
((select pivot am `CM?.mult m` xsdenote eq m am (sort hi)) `CM?.mult m` xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))));
partition_length (bool_of_compare f pivot) q;
sort_correct_aux eq m am hi;
sort_correct_aux eq m am lo;
EQ?.symmetry eq (xsdenote eq m am lo) (xsdenote eq m am (sort lo));
EQ?.symmetry eq (xsdenote eq m am hi) (xsdenote eq m am (sort hi));
CM?.congruence m
(xsdenote eq m am (sort hi))
(xsdenote eq m am (sort lo))
(xsdenote eq m am hi)
(xsdenote eq m am lo);
assert (EQ?.eq eq
(xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo));
EQ?.reflexivity eq (select pivot am);
CM?.congruence m
(select pivot am)
(xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))
(select pivot am)
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo);
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)));
partition_equiv eq m am pivot q;
CM?.congruence m
(select pivot am)
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select pivot am)
(xsdenote eq m am q);
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo))
(select pivot am `CM?.mult m` (xsdenote eq m am q));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am q)));
lemma_xsdenote_aux eq m am pivot q;
EQ?.symmetry eq
(xsdenote eq m am (pivot::q))
(select pivot am `CM?.mult m` (xsdenote eq m am q));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am q))
(xsdenote eq m am xs);
EQ?.symmetry eq (xsdenote eq m am (sort xs)) (xsdenote eq m am xs)
#pop-options
#push-options "--fuel 0 --ifuel 0"
(* Lemmas to be called after a permutation compatible with AC-unification was found *)
let smt_reflexivity (#a:Type) (eq:CE.equiv a) (x y:a)
: Lemma (requires x == y)
(ensures CE.EQ?.eq eq x y)
= CE.EQ?.reflexivity eq x
let identity_left_smt (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x y:a)
: Lemma
(requires x == y)
(ensures CE.EQ?.eq eq x (CE.CM?.mult m (CE.CM?.unit m) y))
= CE.CM?.identity m x;
CE.EQ?.symmetry eq (CE.CM?.mult m (CE.CM?.unit m) x) x
let identity_left (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x:a)
: Lemma (CE.EQ?.eq eq x (CE.CM?.mult m (CE.CM?.unit m) x))
= CE.CM?.identity m x;
CE.EQ?.symmetry eq (CE.CM?.mult m (CE.CM?.unit m) x) x
let identity_right_diff (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x y:a) : Lemma
(requires CE.EQ?.eq eq x y)
(ensures CE.EQ?.eq eq (CE.CM?.mult m x (CE.CM?.unit m)) y)
= CE.right_identity eq m x;
CE.EQ?.transitivity eq (CE.CM?.mult m x (CE.CM?.unit m)) x y
/// Dismiss possible vprops goals that might have been created by lemma application.
/// These vprops will be instantiated at a later stage; else, Meta-F* will raise an error
let rec dismiss_slprops () : Tac unit =
match term_as_formula' (cur_goal ()) with
| App t _ -> if is_fvar t (`%squash) then () else (dismiss(); dismiss_slprops ())
| _ -> dismiss(); dismiss_slprops ()
/// Recursively removing trailing empty assertions
let rec n_identity_left (n:int) (eq m:term) : Tac unit
= if n = 0 then (
apply_lemma (`(CE.EQ?.reflexivity (`#eq)));
// Cleaning up, in case a uvar has been generated here. It'll be solved later
set_goals [])
else (
apply_lemma (`identity_right_diff (`#eq) (`#m));
// Drop the slprops generated, they will be solved later
dismiss_slprops ();
n_identity_left (n-1) eq m
)
/// Helper lemma: If two vprops (as represented by lists of atoms) are equivalent, then their canonical forms
/// (corresponding to applying the sort function on atoms) are equivalent
let equivalent_sorted (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (l1 l2 l1' l2':list atom)
: Lemma (requires
sort l1 == sort l1' /\
sort l2 == sort l2' /\
xsdenote eq m am l1 `CE.EQ?.eq eq` xsdenote eq m am l2)
(ensures xsdenote eq m am l1' `CE.EQ?.eq eq` xsdenote eq m am l2')
= let open FStar.Algebra.CommMonoid.Equiv in
sort_correct_aux eq m am l1';
sort_correct_aux eq m am l1;
EQ?.symmetry eq (xsdenote eq m am l1) (xsdenote eq m am (sort l1));
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am (sort l1'))
(xsdenote eq m am l1);
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am l1)
(xsdenote eq m am l2);
sort_correct_aux eq m am l2;
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am l2)
(xsdenote eq m am (sort l2));
sort_correct_aux eq m am l2';
EQ?.symmetry eq (xsdenote eq m am l2') (xsdenote eq m am (sort l2'));
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am (sort l2))
(xsdenote eq m am l2')
#pop-options
/// Finds the position of first occurrence of x in xs.
/// This is now specialized to terms and their funny term_eq_old.
let rec where_aux (n:nat) (x:term) (xs:list term) :
Tac (option nat) (decreases xs) =
match xs with
| [] -> None
| x'::xs' -> if term_eq_old x x' then Some n else where_aux (n+1) x xs'
let where = where_aux 0
let fatom (t:term) (ts:list term) (am:amap term) : Tac (exp * list term * amap term) =
match where t ts with
| Some v -> (Atom v, ts, am)
| None ->
let vfresh = List.Tot.Base.length ts in
let t = norm_term [iota; zeta] t in
(Atom vfresh, ts `List.Tot.append` [t], update vfresh t am)
/// Transforming a term into the corresponding list of atoms
/// If the atomic terms were already present in the map [am], then
/// they correspond to the same atoms
/// This expects that mult, unit, and t have already been normalized
let rec reification_aux (ts:list term) (am:amap term)
(mult unit t : term) : Tac (exp * list term * amap term) =
let hd, tl = collect_app_ref t in
match inspect_unascribe hd, List.Tot.Base.list_unref tl with
| Tv_FVar fv, [(t1, Q_Explicit) ; (t2, Q_Explicit)] ->
if term_eq_old (pack (Tv_FVar fv)) mult
then (let (e1, ts, am) = reification_aux ts am mult unit t1 in
let (e2, ts, am) = reification_aux ts am mult unit t2 in
(Mult e1 e2, ts, am))
else fatom t ts am
| _, _ ->
if term_eq_old t unit
then (Unit, ts, am)
else fatom t ts am
/// Performs the required normalization before calling the function above
let reification (eq: term) (m: term) (ts:list term) (am:amap term) (t:term) :
Tac (exp * list term * amap term) =
let mult = norm_term [iota; zeta; delta] (`CE.CM?.mult (`#m)) in
let unit = norm_term [iota; zeta; delta] (`CE.CM?.unit (`#m)) in
let t = norm_term [iota; zeta] t in
reification_aux ts am mult unit t
/// Meta-F* internal: Transforms the atom map into a term
let rec convert_map (m : list (atom * term)) : term =
match m with
| [] -> `[]
| (a, t)::ps ->
let a = pack (Tv_Const (C_Int a)) in
(* let t = norm_term [delta] t in *)
`((`#a, (`#t)) :: (`#(convert_map ps)))
/// `am` is an amap (basically a list) of terms, each representing a value
/// of type `a` (whichever we are canonicalizing). This functions converts | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Reflection.V2.Derived.Lemmas",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | am: Steel.Effect.Common.amap FStar.Tactics.NamedView.term -> FStar.Tactics.NamedView.term | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.amap",
"FStar.Tactics.NamedView.term",
"Prims.list",
"FStar.Pervasives.Native.tuple2",
"Steel.Effect.Common.atom",
"FStar.Stubs.Reflection.Types.term",
"Steel.Effect.Common.convert_map"
] | [] | false | false | false | true | false | let convert_am (am: amap term) : term =
| let map, def = am in
`(((`#(convert_map map)), (`#def))) | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.equivalent_lists' | val equivalent_lists' (n: nat) (use_smt: bool) (l1 l2 l1_del l2_del: list atom) (am: amap term)
: Tac (list atom * list atom * bool * list term) | val equivalent_lists' (n: nat) (use_smt: bool) (l1 l2 l1_del l2_del: list atom) (am: amap term)
: Tac (list atom * list atom * bool * list term) | let rec equivalent_lists' (n:nat) (use_smt:bool) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, []
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Try to make progress by matching non-uvars of l2 with candidates in l1
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Should always be smaller or equal to n
// If it is equal, no progress was made.
if use_smt then
// SMT fallback is allowed
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 70,
"end_line": 1067,
"start_col": 0,
"start_line": 1028
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am
/// Iterates over all terms in [l2] to prepare them for unification with SMT rewriting
let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2
/// Recursively calls equivalent_lists_once.
/// Stops when we're done with unification, or when we didn't make any progress
/// If we didn't make any progress, we have too many candidates for some terms.
/// Accumulates rewritings of l1 and l2 in l1_del and l2_del, with the invariant
/// that the two lists are unifiable at any point | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
n: Prims.nat ->
use_smt: Prims.bool ->
l1: Prims.list Steel.Effect.Common.atom ->
l2: Prims.list Steel.Effect.Common.atom ->
l1_del: Prims.list Steel.Effect.Common.atom ->
l2_del: Prims.list Steel.Effect.Common.atom ->
am: Steel.Effect.Common.amap FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac
(((Prims.list Steel.Effect.Common.atom * Prims.list Steel.Effect.Common.atom) * Prims.bool) *
Prims.list FStar.Tactics.NamedView.term) | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.nat",
"Prims.bool",
"Prims.list",
"Steel.Effect.Common.atom",
"Steel.Effect.Common.amap",
"FStar.Tactics.NamedView.term",
"FStar.Pervasives.Native.Mktuple4",
"Prims.Nil",
"FStar.Pervasives.Native.tuple4",
"Prims.Cons",
"FStar.Tactics.V2.Derived.fail",
"Prims.string",
"Prims.op_Hat",
"FStar.Stubs.Tactics.V2.Builtins.term_to_string",
"Steel.Effect.Common.get_head",
"Steel.Effect.Common.is_uvar",
"Steel.Effect.Common.select",
"FStar.List.Tot.Base.append",
"Prims.unit",
"Steel.Effect.Common.try_unifying_remaining",
"Prims.op_GreaterThanOrEqual",
"FStar.Pervasives.Native.tuple3",
"Steel.Effect.Common.equivalent_lists_fallback",
"FStar.Pervasives.Native.tuple2",
"Steel.Effect.Common.replace_smt_uvars",
"Steel.Effect.Common.fail_atoms",
"Steel.Effect.Common.equivalent_lists'",
"FStar.List.Tot.Base.length",
"Steel.Effect.Common.equivalent_lists_once_l2",
"Steel.Effect.Common.equivalent_lists_once",
"Steel.Effect.Common.is_only_uvar"
] | [
"recursion"
] | false | true | false | false | false | let rec equivalent_lists' (n: nat) (use_smt: bool) (l1 l2 l1_del l2_del: list atom) (am: amap term)
: Tac (list atom * list atom * bool * list term) =
| match l1 with
| [] ->
(match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
if is_uvar (select hd am)
then ((l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am)))
| _ ->
if is_only_uvar l2 am
then
(try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, [])
else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n
then
(let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n
then
(if use_smt
then
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am)
else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am)
else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.try_candidates | val try_candidates (t: atom) (l: list atom) (am: amap term) : Tac (atom * int) | val try_candidates (t: atom) (l: list atom) (am: amap term) : Tac (atom * int) | let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n' | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 55,
"end_line": 854,
"start_col": 0,
"start_line": 844
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
t: Steel.Effect.Common.atom ->
l: Prims.list Steel.Effect.Common.atom ->
am: Steel.Effect.Common.amap FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac (Steel.Effect.Common.atom * Prims.int) | FStar.Tactics.Effect.Tac | [] | [] | [
"Steel.Effect.Common.atom",
"Prims.list",
"Steel.Effect.Common.amap",
"FStar.Tactics.NamedView.term",
"FStar.Pervasives.Native.Mktuple2",
"Prims.int",
"FStar.Pervasives.Native.tuple2",
"Steel.Effect.Common.try_candidates",
"Prims.bool",
"Prims.op_AmpAmp",
"Prims.op_disEquality",
"Prims.op_Addition",
"FStar.Tactics.V2.Derived.try_with",
"Prims.unit",
"FStar.Tactics.Effect.raise",
"Steel.Effect.Common.Success",
"Steel.Effect.Common.Failed",
"FStar.Tactics.V2.Derived.unify",
"Steel.Effect.Common.select",
"Prims.exn",
"Steel.Effect.Common.is_uvar"
] | [
"recursion"
] | false | true | false | false | false | let rec try_candidates (t: atom) (l: list atom) (am: amap term) : Tac (atom * int) =
| match l with
| [] -> t, 0
| hd :: tl ->
if is_uvar (select hd am)
then (try_candidates t tl am)
else
let res =
try if unify (select t am) (select hd am) then raise Success else raise Failed
with
| Success -> true
| _ -> false
in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n' | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.get_head | val get_head (l: list atom) (am: amap term) : term | val get_head (l: list atom) (am: amap term) : term | let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 25,
"end_line": 912,
"start_col": 0,
"start_line": 910
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
) | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list Steel.Effect.Common.atom -> am: Steel.Effect.Common.amap FStar.Tactics.NamedView.term
-> FStar.Tactics.NamedView.term | Prims.Tot | [
"total"
] | [] | [
"Prims.list",
"Steel.Effect.Common.atom",
"Steel.Effect.Common.amap",
"FStar.Tactics.NamedView.term",
"Steel.Effect.Common.select"
] | [] | false | false | false | true | false | let get_head (l: list atom) (am: amap term) : term =
| match l with
| [] -> `()
| hd :: _ -> select hd am | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.is_only_uvar | val is_only_uvar (l: list atom) (am: amap term) : Tac bool | val is_only_uvar (l: list atom) (am: amap term) : Tac bool | let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 12,
"end_line": 917,
"start_col": 0,
"start_line": 915
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | l: Prims.list Steel.Effect.Common.atom -> am: Steel.Effect.Common.amap FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac Prims.bool | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.list",
"Steel.Effect.Common.atom",
"Steel.Effect.Common.amap",
"FStar.Tactics.NamedView.term",
"Prims.op_Equality",
"Prims.int",
"FStar.List.Tot.Base.length",
"Steel.Effect.Common.is_uvar",
"Steel.Effect.Common.select",
"FStar.List.Tot.Base.hd",
"Prims.bool"
] | [] | false | true | false | false | false | let is_only_uvar (l: list atom) (am: amap term) : Tac bool =
| if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am) else false | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.equivalent_lists_once | val equivalent_lists_once (l1 l2 l1_del l2_del: list atom) (am: amap term)
: Tac (list atom * list atom * list atom * list atom) | val equivalent_lists_once (l1 l2 l1_del l2_del: list atom) (am: amap term)
: Tac (list atom * list atom * list atom * list atom) | let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 5,
"end_line": 880,
"start_col": 0,
"start_line": 867
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved. | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
l1: Prims.list Steel.Effect.Common.atom ->
l2: Prims.list Steel.Effect.Common.atom ->
l1_del: Prims.list Steel.Effect.Common.atom ->
l2_del: Prims.list Steel.Effect.Common.atom ->
am: Steel.Effect.Common.amap FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac
(((Prims.list Steel.Effect.Common.atom * Prims.list Steel.Effect.Common.atom) *
Prims.list Steel.Effect.Common.atom) *
Prims.list Steel.Effect.Common.atom) | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.list",
"Steel.Effect.Common.atom",
"Steel.Effect.Common.amap",
"FStar.Tactics.NamedView.term",
"FStar.Pervasives.Native.Mktuple4",
"Prims.Nil",
"FStar.Pervasives.Native.tuple4",
"Prims.int",
"Prims.op_Equality",
"Steel.Effect.Common.equivalent_lists_once",
"Prims.Cons",
"Steel.Effect.Common.remove_from_list",
"Prims.bool",
"FStar.Pervasives.Native.tuple2",
"Steel.Effect.Common.try_candidates"
] | [
"recursion"
] | false | true | false | false | false | let rec equivalent_lists_once (l1 l2 l1_del l2_del: list atom) (am: amap term)
: Tac (list atom * list atom * list atom * list atom) =
| match l1 with
| [] -> [], l2, l1_del, l2_del
| hd :: tl ->
let t, n = try_candidates hd l2 am in
if n = 1
then
(let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd :: l1_del) (t :: l2_del) am)
else
(let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd :: rem1, rem2, l1'_del, l2'_del) | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.unifies_with_all_uvars | val unifies_with_all_uvars (t: term) (l: list atom) (am: amap term) : Tac bool | val unifies_with_all_uvars (t: term) (l: list atom) (am: amap term) : Tac bool | let rec unifies_with_all_uvars (t:term) (l:list atom) (am:amap term) : Tac bool =
match l with
| [] -> true
| hd::tl ->
if unifies_with_all_uvars t tl am then (
// Unified with tail, try this term
let hd_t = select hd am in
if is_uvar hd_t then (
// The head term is a uvar, try unifying
try if unify t hd_t then raise Success else raise Failed
with | Success -> true | _ -> false
) else true // The uvar is not a head term, we do not need to try it
) else false | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 18,
"end_line": 1082,
"start_col": 0,
"start_line": 1070
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am
/// Iterates over all terms in [l2] to prepare them for unification with SMT rewriting
let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2
/// Recursively calls equivalent_lists_once.
/// Stops when we're done with unification, or when we didn't make any progress
/// If we didn't make any progress, we have too many candidates for some terms.
/// Accumulates rewritings of l1 and l2 in l1_del and l2_del, with the invariant
/// that the two lists are unifiable at any point
/// The boolean indicates if there is a leftover empty frame
let rec equivalent_lists' (n:nat) (use_smt:bool) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, []
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Try to make progress by matching non-uvars of l2 with candidates in l1
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Should always be smaller or equal to n
// If it is equal, no progress was made.
if use_smt then
// SMT fallback is allowed
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
t: FStar.Tactics.NamedView.term ->
l: Prims.list Steel.Effect.Common.atom ->
am: Steel.Effect.Common.amap FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac Prims.bool | FStar.Tactics.Effect.Tac | [] | [] | [
"FStar.Tactics.NamedView.term",
"Prims.list",
"Steel.Effect.Common.atom",
"Steel.Effect.Common.amap",
"Prims.bool",
"FStar.Tactics.V2.Derived.try_with",
"Prims.unit",
"FStar.Tactics.Effect.raise",
"Steel.Effect.Common.Success",
"Steel.Effect.Common.Failed",
"FStar.Tactics.V2.Derived.unify",
"Prims.exn",
"Steel.Effect.Common.is_uvar",
"Steel.Effect.Common.select",
"Steel.Effect.Common.unifies_with_all_uvars"
] | [
"recursion"
] | false | true | false | false | false | let rec unifies_with_all_uvars (t: term) (l: list atom) (am: amap term) : Tac bool =
| match l with
| [] -> true
| hd :: tl ->
if unifies_with_all_uvars t tl am
then
(let hd_t = select hd am in
if is_uvar hd_t
then
(try if unify t hd_t then raise Success else raise Failed
with
| Success -> true
| _ -> false)
else true)
else false | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.reification_aux | val reification_aux (ts: list term) (am: amap term) (mult unit t: term)
: Tac (exp * list term * amap term) | val reification_aux (ts: list term) (am: amap term) (mult unit t: term)
: Tac (exp * list term * amap term) | let rec reification_aux (ts:list term) (am:amap term)
(mult unit t : term) : Tac (exp * list term * amap term) =
let hd, tl = collect_app_ref t in
match inspect_unascribe hd, List.Tot.Base.list_unref tl with
| Tv_FVar fv, [(t1, Q_Explicit) ; (t2, Q_Explicit)] ->
if term_eq_old (pack (Tv_FVar fv)) mult
then (let (e1, ts, am) = reification_aux ts am mult unit t1 in
let (e2, ts, am) = reification_aux ts am mult unit t2 in
(Mult e1 e2, ts, am))
else fatom t ts am
| _, _ ->
if term_eq_old t unit
then (Unit, ts, am)
else fatom t ts am | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 22,
"end_line": 1640,
"start_col": 0,
"start_line": 1627
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am
/// Iterates over all terms in [l2] to prepare them for unification with SMT rewriting
let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2
/// Recursively calls equivalent_lists_once.
/// Stops when we're done with unification, or when we didn't make any progress
/// If we didn't make any progress, we have too many candidates for some terms.
/// Accumulates rewritings of l1 and l2 in l1_del and l2_del, with the invariant
/// that the two lists are unifiable at any point
/// The boolean indicates if there is a leftover empty frame
let rec equivalent_lists' (n:nat) (use_smt:bool) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, []
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Try to make progress by matching non-uvars of l2 with candidates in l1
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Should always be smaller or equal to n
// If it is equal, no progress was made.
if use_smt then
// SMT fallback is allowed
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
/// Checks if term for atom t unifies with fall uvars in l
let rec unifies_with_all_uvars (t:term) (l:list atom) (am:amap term) : Tac bool =
match l with
| [] -> true
| hd::tl ->
if unifies_with_all_uvars t tl am then (
// Unified with tail, try this term
let hd_t = select hd am in
if is_uvar hd_t then (
// The head term is a uvar, try unifying
try if unify t hd_t then raise Success else raise Failed
with | Success -> true | _ -> false
) else true // The uvar is not a head term, we do not need to try it
) else false
/// Puts all terms in l1 that cannot unify with the uvars in l2 at the top:
/// They need to be solved first
let rec most_restricted_at_top (l1 l2:list atom) (am:amap term) : Tac (list atom) =
match l1 with
| [] -> []
| hd::tl ->
if unifies_with_all_uvars (select hd am) l2 am then (most_restricted_at_top tl l2 am) `List.Tot.append` [hd]
else hd::(most_restricted_at_top tl l2 am)
/// Core AC-unification tactic.
/// First remove all trivially equal terms, then try to decide equivalence.
/// Assumes that l1 does not contain any vprop uvar.
/// If it succeeds, returns permutations of l1, l2, and a boolean indicating
/// if l2 has a trailing empty frame to be unified
let equivalent_lists (use_smt:bool) (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term)
= let l1, l2, l1_del, l2_del = trivial_cancels l1 l2 am in
let l1 = most_restricted_at_top l1 l2 am in
let n = List.Tot.length l1 in
let l1_del, l2_del, emp_frame, uvar_terms = equivalent_lists' n use_smt l1 l2 l1_del l2_del am in
l1_del, l2_del, emp_frame, uvar_terms
(* Helpers to relate the actual terms to their representation as a list of atoms *)
open FStar.Reflection.V2.Derived.Lemmas
let rec list_to_string (l:list term) : Tac string =
match l with
| [] -> "end"
| hd::tl -> term_to_string hd ^ " " ^ list_to_string tl
let rec mdenote_gen (#a:Type u#aa) (unit:a) (mult:a -> a -> a) (am:amap a) (e:exp) : a =
match e with
| Unit -> unit
| Atom x -> select x am
| Mult e1 e2 -> mult (mdenote_gen unit mult am e1) (mdenote_gen unit mult am e2)
let rec xsdenote_gen (#a:Type) (unit:a) (mult:a -> a -> a) (am:amap a) (xs:list atom) : a =
match xs with
| [] -> unit
| [x] -> select x am
| x::xs' -> mult (select x am) (xsdenote_gen unit mult am xs')
unfold
let mdenote (#a:Type u#aa) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp) : a =
let open FStar.Algebra.CommMonoid.Equiv in
mdenote_gen (CM?.unit m) (CM?.mult m) am e
unfold
let xsdenote (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom) : a =
let open FStar.Algebra.CommMonoid.Equiv in
xsdenote_gen (CM?.unit m) (CM?.mult m) am xs
// We reimplement List.Tot.Base.append because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_append (#t: Type) (l1 l2: list t) : Pure (list t)
(requires True)
(ensures (fun res -> res == l1 `List.Tot.append` l2))
(decreases l1)
= match l1 with
| [] -> l2
| a :: q -> a :: my_append q l2
let rec flatten (e:exp) : list atom =
match e with
| Unit -> []
| Atom x -> [x]
| Mult e1 e2 -> flatten e1 `my_append` flatten e2
let rec flatten_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs1 xs2:list atom)
: Lemma (xsdenote eq m am (xs1 `my_append` xs2) `CE.EQ?.eq eq` CE.CM?.mult m (xsdenote eq m am xs1)
(xsdenote eq m am xs2)) =
let open FStar.Algebra.CommMonoid.Equiv in
match xs1 with
| [] ->
CM?.identity m (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.unit m) (xsdenote eq m am xs2)) (xsdenote eq m am xs2)
| [x] -> (
if (Nil? xs2)
then (right_identity eq m (select x am);
EQ?.symmetry eq (CM?.mult m (select x am) (CM?.unit m)) (select x am))
else EQ?.reflexivity eq (CM?.mult m (xsdenote eq m am [x]) (xsdenote eq m am xs2)))
| x::xs1' ->
flatten_correct_aux eq m am xs1' xs2;
EQ?.reflexivity eq (select x am);
CM?.congruence m (select x am) (xsdenote eq m am (xs1' `my_append` xs2))
(select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2));
CM?.associativity m (select x am) (xsdenote eq m am xs1') (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)));
EQ?.transitivity eq (CM?.mult m (select x am) (xsdenote eq m am (xs1' `my_append` xs2)))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)))
(CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
let rec flatten_correct (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp)
: Lemma (mdenote eq m am e `CE.EQ?.eq eq` xsdenote eq m am (flatten e)) =
let open FStar.Algebra.CommMonoid.Equiv in
match e with
| Unit -> EQ?.reflexivity eq (CM?.unit m)
| Atom x -> EQ?.reflexivity eq (select x am)
| Mult e1 e2 ->
flatten_correct_aux eq m am (flatten e1) (flatten e2);
EQ?.symmetry eq (xsdenote eq m am (flatten e1 `my_append` flatten e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)));
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CM?.congruence m (mdenote eq m am e1) (mdenote eq m am e2)
(xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2));
EQ?.transitivity eq (CM?.mult m (mdenote eq m am e1) (mdenote eq m am e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)))
(xsdenote eq m am (flatten e1 `my_append` flatten e2))
let monoid_reflect (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e1 e2:exp)
(_ : squash (xsdenote eq m am (flatten e1) `CE.EQ?.eq eq` xsdenote eq m am (flatten e2)))
: squash (mdenote eq m am e1 `CE.EQ?.eq eq` mdenote eq m am e2) =
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CE.EQ?.symmetry eq (mdenote eq m am e2) (xsdenote eq m am (flatten e2));
CE.EQ?.transitivity eq
(xsdenote eq m am (flatten e1))
(xsdenote eq m am (flatten e2))
(mdenote eq m am e2);
CE.EQ?.transitivity eq
(mdenote eq m am e1)
(xsdenote eq m am (flatten e1))
(mdenote eq m am e2)
// Here we sort the variable numbers
// We reimplement List.Tot.Base.sortWith because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_partition (#a: Type) (f: (a -> Tot bool)) (l: list a)
: Pure (list a & list a)
(requires True)
(ensures (fun res -> res == List.Tot.partition f l))
= match l with
| [] -> [], []
| hd::tl ->
let l1, l2 = my_partition f tl in
if f hd
then hd::l1, l2
else l1, hd::l2
let rec partition_ext (#a: Type) (f1 f2: (a -> Tot bool)) (l: list a)
: Lemma
(requires (forall x . f1 x == f2 x))
(ensures (List.Tot.partition f1 l == List.Tot.partition f2 l))
= match l with
| [] -> ()
| hd::tl -> partition_ext f1 f2 tl
let my_bool_of_compare (#a: Type) (f: a -> a -> Tot int) (x: a) (y: a) : Tot bool
= f x y < 0
let rec my_sortWith (#a: Type) (f: (a -> a -> Tot int)) (l:list a)
: Pure (list a)
(requires True)
(ensures (fun res -> res == List.Tot.sortWith f l))
(decreases (List.Tot.length l))
= match l with
| [] -> []
| pivot::tl ->
let hi, lo = my_partition (my_bool_of_compare f pivot) tl in
partition_ext (my_bool_of_compare f pivot) (List.Tot.bool_of_compare f pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f pivot) tl;
my_append (my_sortWith f lo) (pivot::my_sortWith f hi)
let rec sortWith_ext (#a: Type) (f1 f2: (a -> a -> Tot int)) (l: list a)
: Lemma
(requires (forall x y . f1 x y == f2 x y))
(ensures (List.Tot.sortWith f1 l == List.Tot.sortWith f2 l))
(decreases (List.Tot.length l))
= match l with
| [] -> ()
| pivot::tl ->
partition_ext (List.Tot.bool_of_compare f1 pivot) (List.Tot.bool_of_compare f2 pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f1 pivot) tl;
let hi, lo = List.Tot.partition (List.Tot.bool_of_compare f1 pivot) tl in
sortWith_ext f1 f2 lo;
sortWith_ext f1 f2 hi
let permute = list atom -> list atom
let my_compare_of_bool (#a:eqtype) (rel: a -> a -> Tot bool) (x: a) (y: a) : Tot int
= if x `rel` y then -1
else if x = y then 0
else 1
let sort : permute = my_sortWith #int (my_compare_of_bool (<))
#push-options "--fuel 1 --ifuel 1"
let lemma_xsdenote_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (hd:atom) (tl:list atom)
: Lemma (xsdenote eq m am (hd::tl) `CE.EQ?.eq eq`
(CE.CM?.mult m (select hd am) (xsdenote eq m am tl)))
= let open FStar.Algebra.CommMonoid.Equiv in
match tl with
| [] ->
assert (xsdenote eq m am (hd::tl) == select hd am);
CM?.identity m (select hd am);
EQ?.symmetry eq (CM?.unit m `CM?.mult m` select hd am) (select hd am);
CM?.commutativity m (CM?.unit m) (select hd am);
EQ?.transitivity eq
(xsdenote eq m am (hd::tl))
(CM?.unit m `CM?.mult m` select hd am)
(CM?.mult m (select hd am) (xsdenote eq m am tl))
| _ -> EQ?.reflexivity eq (xsdenote eq m am (hd::tl))
let rec partition_equiv (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (pivot:atom) (q:list atom)
: Lemma
(let open FStar.List.Tot.Base in
let hi, lo = partition (bool_of_compare (compare_of_bool (<)) pivot) q in
CE.EQ?.eq eq
(xsdenote eq m am hi `CE.CM?.mult m` xsdenote eq m am lo)
(xsdenote eq m am q))
= let open FStar.Algebra.CommMonoid.Equiv in
let open FStar.List.Tot.Base in
let f = bool_of_compare (compare_of_bool (<)) pivot in
let hi, lo = partition f q in
match q with
| [] -> CM?.identity m (xsdenote eq m am hi)
| hd::tl ->
let l1, l2 = partition f tl in
partition_equiv eq m am pivot tl;
assert (EQ?.eq eq
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(xsdenote eq m am tl));
EQ?.reflexivity eq (xsdenote eq m am l1);
EQ?.reflexivity eq (xsdenote eq m am l2);
EQ?.reflexivity eq (xsdenote eq m am hi);
EQ?.reflexivity eq (xsdenote eq m am lo);
if f hd then begin
assert (hi == hd::l1 /\ lo == l2);
lemma_xsdenote_aux eq m am hd l1;
CM?.congruence m
(xsdenote eq m am hi)
(xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am l1)
(xsdenote eq m am l2);
CM?.associativity m
(select hd am)
(xsdenote eq m am l1)
(xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
((select hd am `CM?.mult m` xsdenote eq m am l1) `CM?.mult m` xsdenote eq m am l2)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2));
EQ?.reflexivity eq (select hd am);
CM?.congruence m
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(select hd am)
(xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2))
(select hd am `CM?.mult m` xsdenote eq m am tl);
lemma_xsdenote_aux eq m am hd tl;
EQ?.symmetry eq
(xsdenote eq m am (hd::tl))
(select hd am `CM?.mult m` xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am tl)
(xsdenote eq m am (hd::tl))
end else begin
assert (hi == l1 /\ lo == hd::l2);
lemma_xsdenote_aux eq m am hd l2;
CM?.congruence m
(xsdenote eq m am hi)
(xsdenote eq m am lo)
(xsdenote eq m am l1)
(select hd am `CM?.mult m` xsdenote eq m am l2);
CM?.commutativity m
(xsdenote eq m am l1)
(select hd am `CM?.mult m` xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(xsdenote eq m am l1 `CM?.mult m` (select hd am `CM?.mult m` xsdenote eq m am l2))
((select hd am `CM?.mult m` xsdenote eq m am l2) `CM?.mult m` xsdenote eq m am l1);
CM?.associativity m
(select hd am)
(xsdenote eq m am l2)
(xsdenote eq m am l1);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
((select hd am `CM?.mult m` xsdenote eq m am l2) `CM?.mult m` xsdenote eq m am l1)
(select hd am `CM?.mult m` (xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1));
CM?.commutativity m (xsdenote eq m am l2) (xsdenote eq m am l1);
EQ?.reflexivity eq (select hd am);
CM?.congruence m
(select hd am)
(xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1)
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1))
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2));
CM?.congruence m
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(select hd am)
(xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2))
(select hd am `CM?.mult m` xsdenote eq m am tl);
lemma_xsdenote_aux eq m am hd tl;
EQ?.symmetry eq
(xsdenote eq m am (hd::tl))
(select hd am `CM?.mult m` xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am tl)
(xsdenote eq m am (hd::tl))
end
let rec sort_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom)
: Lemma (requires True)
(ensures xsdenote eq m am xs `CE.EQ?.eq eq` xsdenote eq m am (sort xs))
(decreases (FStar.List.Tot.Base.length xs))
= let open FStar.Algebra.CommMonoid.Equiv in
match xs with
| [] -> EQ?.reflexivity eq (xsdenote eq m am [])
| pivot::q ->
let sort0 : permute = List.Tot.sortWith #int (List.Tot.compare_of_bool (<)) in
let sort_eq (l: list atom) : Lemma
(sort l == sort0 l)
[SMTPat (sort l)]
= sortWith_ext (my_compare_of_bool (<)) (List.Tot.compare_of_bool (<)) l
in
let open FStar.List.Tot.Base in
let f:int -> int -> int = compare_of_bool (<) in
let hi, lo = partition (bool_of_compare f pivot) q in
flatten_correct_aux eq m am (sort lo) (pivot::sort hi);
assert (xsdenote eq m am (sort xs) `EQ?.eq eq`
CM?.mult m (xsdenote eq m am (sort lo))
(xsdenote eq m am (pivot::sort hi)));
lemma_xsdenote_aux eq m am pivot (sort hi);
EQ?.reflexivity eq (xsdenote eq m am (sort lo));
CM?.congruence m
(xsdenote eq m am (sort lo))
(xsdenote eq m am (pivot::sort hi))
(xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` xsdenote eq m am (sort hi));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` xsdenote eq m am (pivot::sort hi))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi))));
CM?.commutativity m
(xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` xsdenote eq m am (sort hi));
CM?.associativity m
(select pivot am)
(xsdenote eq m am (sort hi))
(xsdenote eq m am (sort lo));
EQ?.transitivity eq
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)))
((select pivot am `CM?.mult m` xsdenote eq m am (sort hi)) `CM?.mult m` xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))));
partition_length (bool_of_compare f pivot) q;
sort_correct_aux eq m am hi;
sort_correct_aux eq m am lo;
EQ?.symmetry eq (xsdenote eq m am lo) (xsdenote eq m am (sort lo));
EQ?.symmetry eq (xsdenote eq m am hi) (xsdenote eq m am (sort hi));
CM?.congruence m
(xsdenote eq m am (sort hi))
(xsdenote eq m am (sort lo))
(xsdenote eq m am hi)
(xsdenote eq m am lo);
assert (EQ?.eq eq
(xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo));
EQ?.reflexivity eq (select pivot am);
CM?.congruence m
(select pivot am)
(xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))
(select pivot am)
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo);
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)));
partition_equiv eq m am pivot q;
CM?.congruence m
(select pivot am)
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select pivot am)
(xsdenote eq m am q);
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo))
(select pivot am `CM?.mult m` (xsdenote eq m am q));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am q)));
lemma_xsdenote_aux eq m am pivot q;
EQ?.symmetry eq
(xsdenote eq m am (pivot::q))
(select pivot am `CM?.mult m` (xsdenote eq m am q));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am q))
(xsdenote eq m am xs);
EQ?.symmetry eq (xsdenote eq m am (sort xs)) (xsdenote eq m am xs)
#pop-options
#push-options "--fuel 0 --ifuel 0"
(* Lemmas to be called after a permutation compatible with AC-unification was found *)
let smt_reflexivity (#a:Type) (eq:CE.equiv a) (x y:a)
: Lemma (requires x == y)
(ensures CE.EQ?.eq eq x y)
= CE.EQ?.reflexivity eq x
let identity_left_smt (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x y:a)
: Lemma
(requires x == y)
(ensures CE.EQ?.eq eq x (CE.CM?.mult m (CE.CM?.unit m) y))
= CE.CM?.identity m x;
CE.EQ?.symmetry eq (CE.CM?.mult m (CE.CM?.unit m) x) x
let identity_left (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x:a)
: Lemma (CE.EQ?.eq eq x (CE.CM?.mult m (CE.CM?.unit m) x))
= CE.CM?.identity m x;
CE.EQ?.symmetry eq (CE.CM?.mult m (CE.CM?.unit m) x) x
let identity_right_diff (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x y:a) : Lemma
(requires CE.EQ?.eq eq x y)
(ensures CE.EQ?.eq eq (CE.CM?.mult m x (CE.CM?.unit m)) y)
= CE.right_identity eq m x;
CE.EQ?.transitivity eq (CE.CM?.mult m x (CE.CM?.unit m)) x y
/// Dismiss possible vprops goals that might have been created by lemma application.
/// These vprops will be instantiated at a later stage; else, Meta-F* will raise an error
let rec dismiss_slprops () : Tac unit =
match term_as_formula' (cur_goal ()) with
| App t _ -> if is_fvar t (`%squash) then () else (dismiss(); dismiss_slprops ())
| _ -> dismiss(); dismiss_slprops ()
/// Recursively removing trailing empty assertions
let rec n_identity_left (n:int) (eq m:term) : Tac unit
= if n = 0 then (
apply_lemma (`(CE.EQ?.reflexivity (`#eq)));
// Cleaning up, in case a uvar has been generated here. It'll be solved later
set_goals [])
else (
apply_lemma (`identity_right_diff (`#eq) (`#m));
// Drop the slprops generated, they will be solved later
dismiss_slprops ();
n_identity_left (n-1) eq m
)
/// Helper lemma: If two vprops (as represented by lists of atoms) are equivalent, then their canonical forms
/// (corresponding to applying the sort function on atoms) are equivalent
let equivalent_sorted (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (l1 l2 l1' l2':list atom)
: Lemma (requires
sort l1 == sort l1' /\
sort l2 == sort l2' /\
xsdenote eq m am l1 `CE.EQ?.eq eq` xsdenote eq m am l2)
(ensures xsdenote eq m am l1' `CE.EQ?.eq eq` xsdenote eq m am l2')
= let open FStar.Algebra.CommMonoid.Equiv in
sort_correct_aux eq m am l1';
sort_correct_aux eq m am l1;
EQ?.symmetry eq (xsdenote eq m am l1) (xsdenote eq m am (sort l1));
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am (sort l1'))
(xsdenote eq m am l1);
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am l1)
(xsdenote eq m am l2);
sort_correct_aux eq m am l2;
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am l2)
(xsdenote eq m am (sort l2));
sort_correct_aux eq m am l2';
EQ?.symmetry eq (xsdenote eq m am l2') (xsdenote eq m am (sort l2'));
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am (sort l2))
(xsdenote eq m am l2')
#pop-options
/// Finds the position of first occurrence of x in xs.
/// This is now specialized to terms and their funny term_eq_old.
let rec where_aux (n:nat) (x:term) (xs:list term) :
Tac (option nat) (decreases xs) =
match xs with
| [] -> None
| x'::xs' -> if term_eq_old x x' then Some n else where_aux (n+1) x xs'
let where = where_aux 0
let fatom (t:term) (ts:list term) (am:amap term) : Tac (exp * list term * amap term) =
match where t ts with
| Some v -> (Atom v, ts, am)
| None ->
let vfresh = List.Tot.Base.length ts in
let t = norm_term [iota; zeta] t in
(Atom vfresh, ts `List.Tot.append` [t], update vfresh t am)
/// Transforming a term into the corresponding list of atoms
/// If the atomic terms were already present in the map [am], then
/// they correspond to the same atoms | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Reflection.V2.Derived.Lemmas",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
ts: Prims.list FStar.Tactics.NamedView.term ->
am: Steel.Effect.Common.amap FStar.Tactics.NamedView.term ->
mult: FStar.Tactics.NamedView.term ->
unit: FStar.Tactics.NamedView.term ->
t: FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac
((Steel.Effect.Common.exp * Prims.list FStar.Tactics.NamedView.term) *
Steel.Effect.Common.amap FStar.Tactics.NamedView.term) | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.list",
"FStar.Tactics.NamedView.term",
"Steel.Effect.Common.amap",
"FStar.Stubs.Reflection.Types.term",
"Prims.l_or",
"Prims.eq2",
"Prims.precedes",
"FStar.Stubs.Reflection.V2.Data.argv",
"FStar.Pervasives.Native.fst",
"FStar.Stubs.Reflection.V2.Data.aqualv",
"FStar.Stubs.Reflection.Types.fv",
"Steel.Effect.Common.exp",
"FStar.Pervasives.Native.Mktuple3",
"Steel.Effect.Common.Mult",
"FStar.Pervasives.Native.tuple3",
"Steel.Effect.Common.reification_aux",
"Prims.bool",
"Steel.Effect.Common.fatom",
"FStar.Stubs.Tactics.V2.Builtins.term_eq_old",
"FStar.Tactics.NamedView.pack",
"FStar.Tactics.NamedView.Tv_FVar",
"FStar.Tactics.NamedView.named_term_view",
"FStar.Pervasives.Native.tuple2",
"Steel.Effect.Common.Unit",
"FStar.Pervasives.Native.Mktuple2",
"FStar.List.Tot.Base.list_unref",
"FStar.Tactics.V2.SyntaxHelpers.inspect_unascribe",
"FStar.Tactics.NamedView.term_view",
"Prims.b2t",
"FStar.Tactics.NamedView.notAscription",
"FStar.Reflection.V2.Derived.Lemmas.collect_app_ref"
] | [
"recursion"
] | false | true | false | false | false | let rec reification_aux (ts: list term) (am: amap term) (mult unit t: term)
: Tac (exp * list term * amap term) =
| let hd, tl = collect_app_ref t in
match inspect_unascribe hd, List.Tot.Base.list_unref tl with
| Tv_FVar fv, [t1, Q_Explicit ; t2, Q_Explicit] ->
if term_eq_old (pack (Tv_FVar fv)) mult
then
(let e1, ts, am = reification_aux ts am mult unit t1 in
let e2, ts, am = reification_aux ts am mult unit t2 in
(Mult e1 e2, ts, am))
else fatom t ts am
| _, _ -> if term_eq_old t unit then (Unit, ts, am) else fatom t ts am | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.normal_tac | val normal_tac (#a: Type) (x: a) : a | val normal_tac (#a: Type) (x: a) : a | let normal_tac (#a:Type) (x:a) : a = FStar.Pervasives.norm normal_tac_steps x | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 77,
"end_line": 1696,
"start_col": 0,
"start_line": 1696
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am
/// Iterates over all terms in [l2] to prepare them for unification with SMT rewriting
let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2
/// Recursively calls equivalent_lists_once.
/// Stops when we're done with unification, or when we didn't make any progress
/// If we didn't make any progress, we have too many candidates for some terms.
/// Accumulates rewritings of l1 and l2 in l1_del and l2_del, with the invariant
/// that the two lists are unifiable at any point
/// The boolean indicates if there is a leftover empty frame
let rec equivalent_lists' (n:nat) (use_smt:bool) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, []
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Try to make progress by matching non-uvars of l2 with candidates in l1
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Should always be smaller or equal to n
// If it is equal, no progress was made.
if use_smt then
// SMT fallback is allowed
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
/// Checks if term for atom t unifies with fall uvars in l
let rec unifies_with_all_uvars (t:term) (l:list atom) (am:amap term) : Tac bool =
match l with
| [] -> true
| hd::tl ->
if unifies_with_all_uvars t tl am then (
// Unified with tail, try this term
let hd_t = select hd am in
if is_uvar hd_t then (
// The head term is a uvar, try unifying
try if unify t hd_t then raise Success else raise Failed
with | Success -> true | _ -> false
) else true // The uvar is not a head term, we do not need to try it
) else false
/// Puts all terms in l1 that cannot unify with the uvars in l2 at the top:
/// They need to be solved first
let rec most_restricted_at_top (l1 l2:list atom) (am:amap term) : Tac (list atom) =
match l1 with
| [] -> []
| hd::tl ->
if unifies_with_all_uvars (select hd am) l2 am then (most_restricted_at_top tl l2 am) `List.Tot.append` [hd]
else hd::(most_restricted_at_top tl l2 am)
/// Core AC-unification tactic.
/// First remove all trivially equal terms, then try to decide equivalence.
/// Assumes that l1 does not contain any vprop uvar.
/// If it succeeds, returns permutations of l1, l2, and a boolean indicating
/// if l2 has a trailing empty frame to be unified
let equivalent_lists (use_smt:bool) (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term)
= let l1, l2, l1_del, l2_del = trivial_cancels l1 l2 am in
let l1 = most_restricted_at_top l1 l2 am in
let n = List.Tot.length l1 in
let l1_del, l2_del, emp_frame, uvar_terms = equivalent_lists' n use_smt l1 l2 l1_del l2_del am in
l1_del, l2_del, emp_frame, uvar_terms
(* Helpers to relate the actual terms to their representation as a list of atoms *)
open FStar.Reflection.V2.Derived.Lemmas
let rec list_to_string (l:list term) : Tac string =
match l with
| [] -> "end"
| hd::tl -> term_to_string hd ^ " " ^ list_to_string tl
let rec mdenote_gen (#a:Type u#aa) (unit:a) (mult:a -> a -> a) (am:amap a) (e:exp) : a =
match e with
| Unit -> unit
| Atom x -> select x am
| Mult e1 e2 -> mult (mdenote_gen unit mult am e1) (mdenote_gen unit mult am e2)
let rec xsdenote_gen (#a:Type) (unit:a) (mult:a -> a -> a) (am:amap a) (xs:list atom) : a =
match xs with
| [] -> unit
| [x] -> select x am
| x::xs' -> mult (select x am) (xsdenote_gen unit mult am xs')
unfold
let mdenote (#a:Type u#aa) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp) : a =
let open FStar.Algebra.CommMonoid.Equiv in
mdenote_gen (CM?.unit m) (CM?.mult m) am e
unfold
let xsdenote (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom) : a =
let open FStar.Algebra.CommMonoid.Equiv in
xsdenote_gen (CM?.unit m) (CM?.mult m) am xs
// We reimplement List.Tot.Base.append because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_append (#t: Type) (l1 l2: list t) : Pure (list t)
(requires True)
(ensures (fun res -> res == l1 `List.Tot.append` l2))
(decreases l1)
= match l1 with
| [] -> l2
| a :: q -> a :: my_append q l2
let rec flatten (e:exp) : list atom =
match e with
| Unit -> []
| Atom x -> [x]
| Mult e1 e2 -> flatten e1 `my_append` flatten e2
let rec flatten_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs1 xs2:list atom)
: Lemma (xsdenote eq m am (xs1 `my_append` xs2) `CE.EQ?.eq eq` CE.CM?.mult m (xsdenote eq m am xs1)
(xsdenote eq m am xs2)) =
let open FStar.Algebra.CommMonoid.Equiv in
match xs1 with
| [] ->
CM?.identity m (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.unit m) (xsdenote eq m am xs2)) (xsdenote eq m am xs2)
| [x] -> (
if (Nil? xs2)
then (right_identity eq m (select x am);
EQ?.symmetry eq (CM?.mult m (select x am) (CM?.unit m)) (select x am))
else EQ?.reflexivity eq (CM?.mult m (xsdenote eq m am [x]) (xsdenote eq m am xs2)))
| x::xs1' ->
flatten_correct_aux eq m am xs1' xs2;
EQ?.reflexivity eq (select x am);
CM?.congruence m (select x am) (xsdenote eq m am (xs1' `my_append` xs2))
(select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2));
CM?.associativity m (select x am) (xsdenote eq m am xs1') (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)));
EQ?.transitivity eq (CM?.mult m (select x am) (xsdenote eq m am (xs1' `my_append` xs2)))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)))
(CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
let rec flatten_correct (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp)
: Lemma (mdenote eq m am e `CE.EQ?.eq eq` xsdenote eq m am (flatten e)) =
let open FStar.Algebra.CommMonoid.Equiv in
match e with
| Unit -> EQ?.reflexivity eq (CM?.unit m)
| Atom x -> EQ?.reflexivity eq (select x am)
| Mult e1 e2 ->
flatten_correct_aux eq m am (flatten e1) (flatten e2);
EQ?.symmetry eq (xsdenote eq m am (flatten e1 `my_append` flatten e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)));
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CM?.congruence m (mdenote eq m am e1) (mdenote eq m am e2)
(xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2));
EQ?.transitivity eq (CM?.mult m (mdenote eq m am e1) (mdenote eq m am e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)))
(xsdenote eq m am (flatten e1 `my_append` flatten e2))
let monoid_reflect (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e1 e2:exp)
(_ : squash (xsdenote eq m am (flatten e1) `CE.EQ?.eq eq` xsdenote eq m am (flatten e2)))
: squash (mdenote eq m am e1 `CE.EQ?.eq eq` mdenote eq m am e2) =
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CE.EQ?.symmetry eq (mdenote eq m am e2) (xsdenote eq m am (flatten e2));
CE.EQ?.transitivity eq
(xsdenote eq m am (flatten e1))
(xsdenote eq m am (flatten e2))
(mdenote eq m am e2);
CE.EQ?.transitivity eq
(mdenote eq m am e1)
(xsdenote eq m am (flatten e1))
(mdenote eq m am e2)
// Here we sort the variable numbers
// We reimplement List.Tot.Base.sortWith because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_partition (#a: Type) (f: (a -> Tot bool)) (l: list a)
: Pure (list a & list a)
(requires True)
(ensures (fun res -> res == List.Tot.partition f l))
= match l with
| [] -> [], []
| hd::tl ->
let l1, l2 = my_partition f tl in
if f hd
then hd::l1, l2
else l1, hd::l2
let rec partition_ext (#a: Type) (f1 f2: (a -> Tot bool)) (l: list a)
: Lemma
(requires (forall x . f1 x == f2 x))
(ensures (List.Tot.partition f1 l == List.Tot.partition f2 l))
= match l with
| [] -> ()
| hd::tl -> partition_ext f1 f2 tl
let my_bool_of_compare (#a: Type) (f: a -> a -> Tot int) (x: a) (y: a) : Tot bool
= f x y < 0
let rec my_sortWith (#a: Type) (f: (a -> a -> Tot int)) (l:list a)
: Pure (list a)
(requires True)
(ensures (fun res -> res == List.Tot.sortWith f l))
(decreases (List.Tot.length l))
= match l with
| [] -> []
| pivot::tl ->
let hi, lo = my_partition (my_bool_of_compare f pivot) tl in
partition_ext (my_bool_of_compare f pivot) (List.Tot.bool_of_compare f pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f pivot) tl;
my_append (my_sortWith f lo) (pivot::my_sortWith f hi)
let rec sortWith_ext (#a: Type) (f1 f2: (a -> a -> Tot int)) (l: list a)
: Lemma
(requires (forall x y . f1 x y == f2 x y))
(ensures (List.Tot.sortWith f1 l == List.Tot.sortWith f2 l))
(decreases (List.Tot.length l))
= match l with
| [] -> ()
| pivot::tl ->
partition_ext (List.Tot.bool_of_compare f1 pivot) (List.Tot.bool_of_compare f2 pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f1 pivot) tl;
let hi, lo = List.Tot.partition (List.Tot.bool_of_compare f1 pivot) tl in
sortWith_ext f1 f2 lo;
sortWith_ext f1 f2 hi
let permute = list atom -> list atom
let my_compare_of_bool (#a:eqtype) (rel: a -> a -> Tot bool) (x: a) (y: a) : Tot int
= if x `rel` y then -1
else if x = y then 0
else 1
let sort : permute = my_sortWith #int (my_compare_of_bool (<))
#push-options "--fuel 1 --ifuel 1"
let lemma_xsdenote_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (hd:atom) (tl:list atom)
: Lemma (xsdenote eq m am (hd::tl) `CE.EQ?.eq eq`
(CE.CM?.mult m (select hd am) (xsdenote eq m am tl)))
= let open FStar.Algebra.CommMonoid.Equiv in
match tl with
| [] ->
assert (xsdenote eq m am (hd::tl) == select hd am);
CM?.identity m (select hd am);
EQ?.symmetry eq (CM?.unit m `CM?.mult m` select hd am) (select hd am);
CM?.commutativity m (CM?.unit m) (select hd am);
EQ?.transitivity eq
(xsdenote eq m am (hd::tl))
(CM?.unit m `CM?.mult m` select hd am)
(CM?.mult m (select hd am) (xsdenote eq m am tl))
| _ -> EQ?.reflexivity eq (xsdenote eq m am (hd::tl))
let rec partition_equiv (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (pivot:atom) (q:list atom)
: Lemma
(let open FStar.List.Tot.Base in
let hi, lo = partition (bool_of_compare (compare_of_bool (<)) pivot) q in
CE.EQ?.eq eq
(xsdenote eq m am hi `CE.CM?.mult m` xsdenote eq m am lo)
(xsdenote eq m am q))
= let open FStar.Algebra.CommMonoid.Equiv in
let open FStar.List.Tot.Base in
let f = bool_of_compare (compare_of_bool (<)) pivot in
let hi, lo = partition f q in
match q with
| [] -> CM?.identity m (xsdenote eq m am hi)
| hd::tl ->
let l1, l2 = partition f tl in
partition_equiv eq m am pivot tl;
assert (EQ?.eq eq
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(xsdenote eq m am tl));
EQ?.reflexivity eq (xsdenote eq m am l1);
EQ?.reflexivity eq (xsdenote eq m am l2);
EQ?.reflexivity eq (xsdenote eq m am hi);
EQ?.reflexivity eq (xsdenote eq m am lo);
if f hd then begin
assert (hi == hd::l1 /\ lo == l2);
lemma_xsdenote_aux eq m am hd l1;
CM?.congruence m
(xsdenote eq m am hi)
(xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am l1)
(xsdenote eq m am l2);
CM?.associativity m
(select hd am)
(xsdenote eq m am l1)
(xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
((select hd am `CM?.mult m` xsdenote eq m am l1) `CM?.mult m` xsdenote eq m am l2)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2));
EQ?.reflexivity eq (select hd am);
CM?.congruence m
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(select hd am)
(xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2))
(select hd am `CM?.mult m` xsdenote eq m am tl);
lemma_xsdenote_aux eq m am hd tl;
EQ?.symmetry eq
(xsdenote eq m am (hd::tl))
(select hd am `CM?.mult m` xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am tl)
(xsdenote eq m am (hd::tl))
end else begin
assert (hi == l1 /\ lo == hd::l2);
lemma_xsdenote_aux eq m am hd l2;
CM?.congruence m
(xsdenote eq m am hi)
(xsdenote eq m am lo)
(xsdenote eq m am l1)
(select hd am `CM?.mult m` xsdenote eq m am l2);
CM?.commutativity m
(xsdenote eq m am l1)
(select hd am `CM?.mult m` xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(xsdenote eq m am l1 `CM?.mult m` (select hd am `CM?.mult m` xsdenote eq m am l2))
((select hd am `CM?.mult m` xsdenote eq m am l2) `CM?.mult m` xsdenote eq m am l1);
CM?.associativity m
(select hd am)
(xsdenote eq m am l2)
(xsdenote eq m am l1);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
((select hd am `CM?.mult m` xsdenote eq m am l2) `CM?.mult m` xsdenote eq m am l1)
(select hd am `CM?.mult m` (xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1));
CM?.commutativity m (xsdenote eq m am l2) (xsdenote eq m am l1);
EQ?.reflexivity eq (select hd am);
CM?.congruence m
(select hd am)
(xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1)
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1))
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2));
CM?.congruence m
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(select hd am)
(xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2))
(select hd am `CM?.mult m` xsdenote eq m am tl);
lemma_xsdenote_aux eq m am hd tl;
EQ?.symmetry eq
(xsdenote eq m am (hd::tl))
(select hd am `CM?.mult m` xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am tl)
(xsdenote eq m am (hd::tl))
end
let rec sort_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom)
: Lemma (requires True)
(ensures xsdenote eq m am xs `CE.EQ?.eq eq` xsdenote eq m am (sort xs))
(decreases (FStar.List.Tot.Base.length xs))
= let open FStar.Algebra.CommMonoid.Equiv in
match xs with
| [] -> EQ?.reflexivity eq (xsdenote eq m am [])
| pivot::q ->
let sort0 : permute = List.Tot.sortWith #int (List.Tot.compare_of_bool (<)) in
let sort_eq (l: list atom) : Lemma
(sort l == sort0 l)
[SMTPat (sort l)]
= sortWith_ext (my_compare_of_bool (<)) (List.Tot.compare_of_bool (<)) l
in
let open FStar.List.Tot.Base in
let f:int -> int -> int = compare_of_bool (<) in
let hi, lo = partition (bool_of_compare f pivot) q in
flatten_correct_aux eq m am (sort lo) (pivot::sort hi);
assert (xsdenote eq m am (sort xs) `EQ?.eq eq`
CM?.mult m (xsdenote eq m am (sort lo))
(xsdenote eq m am (pivot::sort hi)));
lemma_xsdenote_aux eq m am pivot (sort hi);
EQ?.reflexivity eq (xsdenote eq m am (sort lo));
CM?.congruence m
(xsdenote eq m am (sort lo))
(xsdenote eq m am (pivot::sort hi))
(xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` xsdenote eq m am (sort hi));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` xsdenote eq m am (pivot::sort hi))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi))));
CM?.commutativity m
(xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` xsdenote eq m am (sort hi));
CM?.associativity m
(select pivot am)
(xsdenote eq m am (sort hi))
(xsdenote eq m am (sort lo));
EQ?.transitivity eq
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)))
((select pivot am `CM?.mult m` xsdenote eq m am (sort hi)) `CM?.mult m` xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))));
partition_length (bool_of_compare f pivot) q;
sort_correct_aux eq m am hi;
sort_correct_aux eq m am lo;
EQ?.symmetry eq (xsdenote eq m am lo) (xsdenote eq m am (sort lo));
EQ?.symmetry eq (xsdenote eq m am hi) (xsdenote eq m am (sort hi));
CM?.congruence m
(xsdenote eq m am (sort hi))
(xsdenote eq m am (sort lo))
(xsdenote eq m am hi)
(xsdenote eq m am lo);
assert (EQ?.eq eq
(xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo));
EQ?.reflexivity eq (select pivot am);
CM?.congruence m
(select pivot am)
(xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))
(select pivot am)
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo);
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)));
partition_equiv eq m am pivot q;
CM?.congruence m
(select pivot am)
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select pivot am)
(xsdenote eq m am q);
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo))
(select pivot am `CM?.mult m` (xsdenote eq m am q));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am q)));
lemma_xsdenote_aux eq m am pivot q;
EQ?.symmetry eq
(xsdenote eq m am (pivot::q))
(select pivot am `CM?.mult m` (xsdenote eq m am q));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am q))
(xsdenote eq m am xs);
EQ?.symmetry eq (xsdenote eq m am (sort xs)) (xsdenote eq m am xs)
#pop-options
#push-options "--fuel 0 --ifuel 0"
(* Lemmas to be called after a permutation compatible with AC-unification was found *)
let smt_reflexivity (#a:Type) (eq:CE.equiv a) (x y:a)
: Lemma (requires x == y)
(ensures CE.EQ?.eq eq x y)
= CE.EQ?.reflexivity eq x
let identity_left_smt (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x y:a)
: Lemma
(requires x == y)
(ensures CE.EQ?.eq eq x (CE.CM?.mult m (CE.CM?.unit m) y))
= CE.CM?.identity m x;
CE.EQ?.symmetry eq (CE.CM?.mult m (CE.CM?.unit m) x) x
let identity_left (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x:a)
: Lemma (CE.EQ?.eq eq x (CE.CM?.mult m (CE.CM?.unit m) x))
= CE.CM?.identity m x;
CE.EQ?.symmetry eq (CE.CM?.mult m (CE.CM?.unit m) x) x
let identity_right_diff (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x y:a) : Lemma
(requires CE.EQ?.eq eq x y)
(ensures CE.EQ?.eq eq (CE.CM?.mult m x (CE.CM?.unit m)) y)
= CE.right_identity eq m x;
CE.EQ?.transitivity eq (CE.CM?.mult m x (CE.CM?.unit m)) x y
/// Dismiss possible vprops goals that might have been created by lemma application.
/// These vprops will be instantiated at a later stage; else, Meta-F* will raise an error
let rec dismiss_slprops () : Tac unit =
match term_as_formula' (cur_goal ()) with
| App t _ -> if is_fvar t (`%squash) then () else (dismiss(); dismiss_slprops ())
| _ -> dismiss(); dismiss_slprops ()
/// Recursively removing trailing empty assertions
let rec n_identity_left (n:int) (eq m:term) : Tac unit
= if n = 0 then (
apply_lemma (`(CE.EQ?.reflexivity (`#eq)));
// Cleaning up, in case a uvar has been generated here. It'll be solved later
set_goals [])
else (
apply_lemma (`identity_right_diff (`#eq) (`#m));
// Drop the slprops generated, they will be solved later
dismiss_slprops ();
n_identity_left (n-1) eq m
)
/// Helper lemma: If two vprops (as represented by lists of atoms) are equivalent, then their canonical forms
/// (corresponding to applying the sort function on atoms) are equivalent
let equivalent_sorted (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (l1 l2 l1' l2':list atom)
: Lemma (requires
sort l1 == sort l1' /\
sort l2 == sort l2' /\
xsdenote eq m am l1 `CE.EQ?.eq eq` xsdenote eq m am l2)
(ensures xsdenote eq m am l1' `CE.EQ?.eq eq` xsdenote eq m am l2')
= let open FStar.Algebra.CommMonoid.Equiv in
sort_correct_aux eq m am l1';
sort_correct_aux eq m am l1;
EQ?.symmetry eq (xsdenote eq m am l1) (xsdenote eq m am (sort l1));
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am (sort l1'))
(xsdenote eq m am l1);
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am l1)
(xsdenote eq m am l2);
sort_correct_aux eq m am l2;
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am l2)
(xsdenote eq m am (sort l2));
sort_correct_aux eq m am l2';
EQ?.symmetry eq (xsdenote eq m am l2') (xsdenote eq m am (sort l2'));
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am (sort l2))
(xsdenote eq m am l2')
#pop-options
/// Finds the position of first occurrence of x in xs.
/// This is now specialized to terms and their funny term_eq_old.
let rec where_aux (n:nat) (x:term) (xs:list term) :
Tac (option nat) (decreases xs) =
match xs with
| [] -> None
| x'::xs' -> if term_eq_old x x' then Some n else where_aux (n+1) x xs'
let where = where_aux 0
let fatom (t:term) (ts:list term) (am:amap term) : Tac (exp * list term * amap term) =
match where t ts with
| Some v -> (Atom v, ts, am)
| None ->
let vfresh = List.Tot.Base.length ts in
let t = norm_term [iota; zeta] t in
(Atom vfresh, ts `List.Tot.append` [t], update vfresh t am)
/// Transforming a term into the corresponding list of atoms
/// If the atomic terms were already present in the map [am], then
/// they correspond to the same atoms
/// This expects that mult, unit, and t have already been normalized
let rec reification_aux (ts:list term) (am:amap term)
(mult unit t : term) : Tac (exp * list term * amap term) =
let hd, tl = collect_app_ref t in
match inspect_unascribe hd, List.Tot.Base.list_unref tl with
| Tv_FVar fv, [(t1, Q_Explicit) ; (t2, Q_Explicit)] ->
if term_eq_old (pack (Tv_FVar fv)) mult
then (let (e1, ts, am) = reification_aux ts am mult unit t1 in
let (e2, ts, am) = reification_aux ts am mult unit t2 in
(Mult e1 e2, ts, am))
else fatom t ts am
| _, _ ->
if term_eq_old t unit
then (Unit, ts, am)
else fatom t ts am
/// Performs the required normalization before calling the function above
let reification (eq: term) (m: term) (ts:list term) (am:amap term) (t:term) :
Tac (exp * list term * amap term) =
let mult = norm_term [iota; zeta; delta] (`CE.CM?.mult (`#m)) in
let unit = norm_term [iota; zeta; delta] (`CE.CM?.unit (`#m)) in
let t = norm_term [iota; zeta] t in
reification_aux ts am mult unit t
/// Meta-F* internal: Transforms the atom map into a term
let rec convert_map (m : list (atom * term)) : term =
match m with
| [] -> `[]
| (a, t)::ps ->
let a = pack (Tv_Const (C_Int a)) in
(* let t = norm_term [delta] t in *)
`((`#a, (`#t)) :: (`#(convert_map ps)))
/// `am` is an amap (basically a list) of terms, each representing a value
/// of type `a` (whichever we are canonicalizing). This functions converts
/// `am` into a single `term` of type `amap a`, suitable to call `mdenote` with *)
let convert_am (am : amap term) : term =
let (map, def) = am in
(* let def = norm_term [delta] def in *)
`( (`#(convert_map map), `#def) )
/// Transforms a term representatoin into a term through quotation
let rec quote_exp (e:exp) : term =
match e with
| Unit -> (`Unit)
| Mult e1 e2 -> (`Mult (`#(quote_exp e1)) (`#(quote_exp e2)))
| Atom n -> let nt = pack (Tv_Const (C_Int n)) in
(`Atom (`#nt))
let rec quote_atoms (l:list atom) = match l with
| [] -> `[]
| hd::tl -> let nt = pack (Tv_Const (C_Int hd)) in
(`Cons (`#nt) (`#(quote_atoms tl)))
/// Some internal normalization steps to make reflection of vprops into atoms and atom permutation go smoothly.
/// We reimplemented sorting/list functions to normalize our uses without normalizing those introduced by the user.
let normal_tac_steps = [primops; iota; zeta; delta_only [
`%mdenote; `%select;
`%my_assoc; `%my_append;
`%flatten; `%sort;
`%my_sortWith; `%my_partition;
`%my_bool_of_compare; `%my_compare_of_bool;
`%fst; `%__proj__Mktuple2__item___1;
`%snd; `%__proj__Mktuple2__item___2;
`%CE.__proj__CM__item__unit;
`%CE.__proj__CM__item__mult;
`%rm]] | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Reflection.V2.Derived.Lemmas",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | x: a -> a | Prims.Tot | [
"total"
] | [] | [
"FStar.Pervasives.norm",
"Steel.Effect.Common.normal_tac_steps"
] | [] | false | false | false | true | false | let normal_tac (#a: Type) (x: a) : a =
| FStar.Pervasives.norm normal_tac_steps x | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.normal_tac_steps | val normal_tac_steps : Prims.list FStar.Pervasives.norm_step | let normal_tac_steps = [primops; iota; zeta; delta_only [
`%mdenote; `%select;
`%my_assoc; `%my_append;
`%flatten; `%sort;
`%my_sortWith; `%my_partition;
`%my_bool_of_compare; `%my_compare_of_bool;
`%fst; `%__proj__Mktuple2__item___1;
`%snd; `%__proj__Mktuple2__item___2;
`%CE.__proj__CM__item__unit;
`%CE.__proj__CM__item__mult;
`%rm]] | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 16,
"end_line": 1693,
"start_col": 0,
"start_line": 1683
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am
/// Iterates over all terms in [l2] to prepare them for unification with SMT rewriting
let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2
/// Recursively calls equivalent_lists_once.
/// Stops when we're done with unification, or when we didn't make any progress
/// If we didn't make any progress, we have too many candidates for some terms.
/// Accumulates rewritings of l1 and l2 in l1_del and l2_del, with the invariant
/// that the two lists are unifiable at any point
/// The boolean indicates if there is a leftover empty frame
let rec equivalent_lists' (n:nat) (use_smt:bool) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, []
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Try to make progress by matching non-uvars of l2 with candidates in l1
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Should always be smaller or equal to n
// If it is equal, no progress was made.
if use_smt then
// SMT fallback is allowed
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
/// Checks if term for atom t unifies with fall uvars in l
let rec unifies_with_all_uvars (t:term) (l:list atom) (am:amap term) : Tac bool =
match l with
| [] -> true
| hd::tl ->
if unifies_with_all_uvars t tl am then (
// Unified with tail, try this term
let hd_t = select hd am in
if is_uvar hd_t then (
// The head term is a uvar, try unifying
try if unify t hd_t then raise Success else raise Failed
with | Success -> true | _ -> false
) else true // The uvar is not a head term, we do not need to try it
) else false
/// Puts all terms in l1 that cannot unify with the uvars in l2 at the top:
/// They need to be solved first
let rec most_restricted_at_top (l1 l2:list atom) (am:amap term) : Tac (list atom) =
match l1 with
| [] -> []
| hd::tl ->
if unifies_with_all_uvars (select hd am) l2 am then (most_restricted_at_top tl l2 am) `List.Tot.append` [hd]
else hd::(most_restricted_at_top tl l2 am)
/// Core AC-unification tactic.
/// First remove all trivially equal terms, then try to decide equivalence.
/// Assumes that l1 does not contain any vprop uvar.
/// If it succeeds, returns permutations of l1, l2, and a boolean indicating
/// if l2 has a trailing empty frame to be unified
let equivalent_lists (use_smt:bool) (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term)
= let l1, l2, l1_del, l2_del = trivial_cancels l1 l2 am in
let l1 = most_restricted_at_top l1 l2 am in
let n = List.Tot.length l1 in
let l1_del, l2_del, emp_frame, uvar_terms = equivalent_lists' n use_smt l1 l2 l1_del l2_del am in
l1_del, l2_del, emp_frame, uvar_terms
(* Helpers to relate the actual terms to their representation as a list of atoms *)
open FStar.Reflection.V2.Derived.Lemmas
let rec list_to_string (l:list term) : Tac string =
match l with
| [] -> "end"
| hd::tl -> term_to_string hd ^ " " ^ list_to_string tl
let rec mdenote_gen (#a:Type u#aa) (unit:a) (mult:a -> a -> a) (am:amap a) (e:exp) : a =
match e with
| Unit -> unit
| Atom x -> select x am
| Mult e1 e2 -> mult (mdenote_gen unit mult am e1) (mdenote_gen unit mult am e2)
let rec xsdenote_gen (#a:Type) (unit:a) (mult:a -> a -> a) (am:amap a) (xs:list atom) : a =
match xs with
| [] -> unit
| [x] -> select x am
| x::xs' -> mult (select x am) (xsdenote_gen unit mult am xs')
unfold
let mdenote (#a:Type u#aa) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp) : a =
let open FStar.Algebra.CommMonoid.Equiv in
mdenote_gen (CM?.unit m) (CM?.mult m) am e
unfold
let xsdenote (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom) : a =
let open FStar.Algebra.CommMonoid.Equiv in
xsdenote_gen (CM?.unit m) (CM?.mult m) am xs
// We reimplement List.Tot.Base.append because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_append (#t: Type) (l1 l2: list t) : Pure (list t)
(requires True)
(ensures (fun res -> res == l1 `List.Tot.append` l2))
(decreases l1)
= match l1 with
| [] -> l2
| a :: q -> a :: my_append q l2
let rec flatten (e:exp) : list atom =
match e with
| Unit -> []
| Atom x -> [x]
| Mult e1 e2 -> flatten e1 `my_append` flatten e2
let rec flatten_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs1 xs2:list atom)
: Lemma (xsdenote eq m am (xs1 `my_append` xs2) `CE.EQ?.eq eq` CE.CM?.mult m (xsdenote eq m am xs1)
(xsdenote eq m am xs2)) =
let open FStar.Algebra.CommMonoid.Equiv in
match xs1 with
| [] ->
CM?.identity m (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.unit m) (xsdenote eq m am xs2)) (xsdenote eq m am xs2)
| [x] -> (
if (Nil? xs2)
then (right_identity eq m (select x am);
EQ?.symmetry eq (CM?.mult m (select x am) (CM?.unit m)) (select x am))
else EQ?.reflexivity eq (CM?.mult m (xsdenote eq m am [x]) (xsdenote eq m am xs2)))
| x::xs1' ->
flatten_correct_aux eq m am xs1' xs2;
EQ?.reflexivity eq (select x am);
CM?.congruence m (select x am) (xsdenote eq m am (xs1' `my_append` xs2))
(select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2));
CM?.associativity m (select x am) (xsdenote eq m am xs1') (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)));
EQ?.transitivity eq (CM?.mult m (select x am) (xsdenote eq m am (xs1' `my_append` xs2)))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)))
(CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
let rec flatten_correct (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp)
: Lemma (mdenote eq m am e `CE.EQ?.eq eq` xsdenote eq m am (flatten e)) =
let open FStar.Algebra.CommMonoid.Equiv in
match e with
| Unit -> EQ?.reflexivity eq (CM?.unit m)
| Atom x -> EQ?.reflexivity eq (select x am)
| Mult e1 e2 ->
flatten_correct_aux eq m am (flatten e1) (flatten e2);
EQ?.symmetry eq (xsdenote eq m am (flatten e1 `my_append` flatten e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)));
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CM?.congruence m (mdenote eq m am e1) (mdenote eq m am e2)
(xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2));
EQ?.transitivity eq (CM?.mult m (mdenote eq m am e1) (mdenote eq m am e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)))
(xsdenote eq m am (flatten e1 `my_append` flatten e2))
let monoid_reflect (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e1 e2:exp)
(_ : squash (xsdenote eq m am (flatten e1) `CE.EQ?.eq eq` xsdenote eq m am (flatten e2)))
: squash (mdenote eq m am e1 `CE.EQ?.eq eq` mdenote eq m am e2) =
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CE.EQ?.symmetry eq (mdenote eq m am e2) (xsdenote eq m am (flatten e2));
CE.EQ?.transitivity eq
(xsdenote eq m am (flatten e1))
(xsdenote eq m am (flatten e2))
(mdenote eq m am e2);
CE.EQ?.transitivity eq
(mdenote eq m am e1)
(xsdenote eq m am (flatten e1))
(mdenote eq m am e2)
// Here we sort the variable numbers
// We reimplement List.Tot.Base.sortWith because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_partition (#a: Type) (f: (a -> Tot bool)) (l: list a)
: Pure (list a & list a)
(requires True)
(ensures (fun res -> res == List.Tot.partition f l))
= match l with
| [] -> [], []
| hd::tl ->
let l1, l2 = my_partition f tl in
if f hd
then hd::l1, l2
else l1, hd::l2
let rec partition_ext (#a: Type) (f1 f2: (a -> Tot bool)) (l: list a)
: Lemma
(requires (forall x . f1 x == f2 x))
(ensures (List.Tot.partition f1 l == List.Tot.partition f2 l))
= match l with
| [] -> ()
| hd::tl -> partition_ext f1 f2 tl
let my_bool_of_compare (#a: Type) (f: a -> a -> Tot int) (x: a) (y: a) : Tot bool
= f x y < 0
let rec my_sortWith (#a: Type) (f: (a -> a -> Tot int)) (l:list a)
: Pure (list a)
(requires True)
(ensures (fun res -> res == List.Tot.sortWith f l))
(decreases (List.Tot.length l))
= match l with
| [] -> []
| pivot::tl ->
let hi, lo = my_partition (my_bool_of_compare f pivot) tl in
partition_ext (my_bool_of_compare f pivot) (List.Tot.bool_of_compare f pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f pivot) tl;
my_append (my_sortWith f lo) (pivot::my_sortWith f hi)
let rec sortWith_ext (#a: Type) (f1 f2: (a -> a -> Tot int)) (l: list a)
: Lemma
(requires (forall x y . f1 x y == f2 x y))
(ensures (List.Tot.sortWith f1 l == List.Tot.sortWith f2 l))
(decreases (List.Tot.length l))
= match l with
| [] -> ()
| pivot::tl ->
partition_ext (List.Tot.bool_of_compare f1 pivot) (List.Tot.bool_of_compare f2 pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f1 pivot) tl;
let hi, lo = List.Tot.partition (List.Tot.bool_of_compare f1 pivot) tl in
sortWith_ext f1 f2 lo;
sortWith_ext f1 f2 hi
let permute = list atom -> list atom
let my_compare_of_bool (#a:eqtype) (rel: a -> a -> Tot bool) (x: a) (y: a) : Tot int
= if x `rel` y then -1
else if x = y then 0
else 1
let sort : permute = my_sortWith #int (my_compare_of_bool (<))
#push-options "--fuel 1 --ifuel 1"
let lemma_xsdenote_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (hd:atom) (tl:list atom)
: Lemma (xsdenote eq m am (hd::tl) `CE.EQ?.eq eq`
(CE.CM?.mult m (select hd am) (xsdenote eq m am tl)))
= let open FStar.Algebra.CommMonoid.Equiv in
match tl with
| [] ->
assert (xsdenote eq m am (hd::tl) == select hd am);
CM?.identity m (select hd am);
EQ?.symmetry eq (CM?.unit m `CM?.mult m` select hd am) (select hd am);
CM?.commutativity m (CM?.unit m) (select hd am);
EQ?.transitivity eq
(xsdenote eq m am (hd::tl))
(CM?.unit m `CM?.mult m` select hd am)
(CM?.mult m (select hd am) (xsdenote eq m am tl))
| _ -> EQ?.reflexivity eq (xsdenote eq m am (hd::tl))
let rec partition_equiv (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (pivot:atom) (q:list atom)
: Lemma
(let open FStar.List.Tot.Base in
let hi, lo = partition (bool_of_compare (compare_of_bool (<)) pivot) q in
CE.EQ?.eq eq
(xsdenote eq m am hi `CE.CM?.mult m` xsdenote eq m am lo)
(xsdenote eq m am q))
= let open FStar.Algebra.CommMonoid.Equiv in
let open FStar.List.Tot.Base in
let f = bool_of_compare (compare_of_bool (<)) pivot in
let hi, lo = partition f q in
match q with
| [] -> CM?.identity m (xsdenote eq m am hi)
| hd::tl ->
let l1, l2 = partition f tl in
partition_equiv eq m am pivot tl;
assert (EQ?.eq eq
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(xsdenote eq m am tl));
EQ?.reflexivity eq (xsdenote eq m am l1);
EQ?.reflexivity eq (xsdenote eq m am l2);
EQ?.reflexivity eq (xsdenote eq m am hi);
EQ?.reflexivity eq (xsdenote eq m am lo);
if f hd then begin
assert (hi == hd::l1 /\ lo == l2);
lemma_xsdenote_aux eq m am hd l1;
CM?.congruence m
(xsdenote eq m am hi)
(xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am l1)
(xsdenote eq m am l2);
CM?.associativity m
(select hd am)
(xsdenote eq m am l1)
(xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
((select hd am `CM?.mult m` xsdenote eq m am l1) `CM?.mult m` xsdenote eq m am l2)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2));
EQ?.reflexivity eq (select hd am);
CM?.congruence m
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(select hd am)
(xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2))
(select hd am `CM?.mult m` xsdenote eq m am tl);
lemma_xsdenote_aux eq m am hd tl;
EQ?.symmetry eq
(xsdenote eq m am (hd::tl))
(select hd am `CM?.mult m` xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am tl)
(xsdenote eq m am (hd::tl))
end else begin
assert (hi == l1 /\ lo == hd::l2);
lemma_xsdenote_aux eq m am hd l2;
CM?.congruence m
(xsdenote eq m am hi)
(xsdenote eq m am lo)
(xsdenote eq m am l1)
(select hd am `CM?.mult m` xsdenote eq m am l2);
CM?.commutativity m
(xsdenote eq m am l1)
(select hd am `CM?.mult m` xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(xsdenote eq m am l1 `CM?.mult m` (select hd am `CM?.mult m` xsdenote eq m am l2))
((select hd am `CM?.mult m` xsdenote eq m am l2) `CM?.mult m` xsdenote eq m am l1);
CM?.associativity m
(select hd am)
(xsdenote eq m am l2)
(xsdenote eq m am l1);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
((select hd am `CM?.mult m` xsdenote eq m am l2) `CM?.mult m` xsdenote eq m am l1)
(select hd am `CM?.mult m` (xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1));
CM?.commutativity m (xsdenote eq m am l2) (xsdenote eq m am l1);
EQ?.reflexivity eq (select hd am);
CM?.congruence m
(select hd am)
(xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1)
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1))
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2));
CM?.congruence m
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(select hd am)
(xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2))
(select hd am `CM?.mult m` xsdenote eq m am tl);
lemma_xsdenote_aux eq m am hd tl;
EQ?.symmetry eq
(xsdenote eq m am (hd::tl))
(select hd am `CM?.mult m` xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am tl)
(xsdenote eq m am (hd::tl))
end
let rec sort_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom)
: Lemma (requires True)
(ensures xsdenote eq m am xs `CE.EQ?.eq eq` xsdenote eq m am (sort xs))
(decreases (FStar.List.Tot.Base.length xs))
= let open FStar.Algebra.CommMonoid.Equiv in
match xs with
| [] -> EQ?.reflexivity eq (xsdenote eq m am [])
| pivot::q ->
let sort0 : permute = List.Tot.sortWith #int (List.Tot.compare_of_bool (<)) in
let sort_eq (l: list atom) : Lemma
(sort l == sort0 l)
[SMTPat (sort l)]
= sortWith_ext (my_compare_of_bool (<)) (List.Tot.compare_of_bool (<)) l
in
let open FStar.List.Tot.Base in
let f:int -> int -> int = compare_of_bool (<) in
let hi, lo = partition (bool_of_compare f pivot) q in
flatten_correct_aux eq m am (sort lo) (pivot::sort hi);
assert (xsdenote eq m am (sort xs) `EQ?.eq eq`
CM?.mult m (xsdenote eq m am (sort lo))
(xsdenote eq m am (pivot::sort hi)));
lemma_xsdenote_aux eq m am pivot (sort hi);
EQ?.reflexivity eq (xsdenote eq m am (sort lo));
CM?.congruence m
(xsdenote eq m am (sort lo))
(xsdenote eq m am (pivot::sort hi))
(xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` xsdenote eq m am (sort hi));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` xsdenote eq m am (pivot::sort hi))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi))));
CM?.commutativity m
(xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` xsdenote eq m am (sort hi));
CM?.associativity m
(select pivot am)
(xsdenote eq m am (sort hi))
(xsdenote eq m am (sort lo));
EQ?.transitivity eq
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)))
((select pivot am `CM?.mult m` xsdenote eq m am (sort hi)) `CM?.mult m` xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))));
partition_length (bool_of_compare f pivot) q;
sort_correct_aux eq m am hi;
sort_correct_aux eq m am lo;
EQ?.symmetry eq (xsdenote eq m am lo) (xsdenote eq m am (sort lo));
EQ?.symmetry eq (xsdenote eq m am hi) (xsdenote eq m am (sort hi));
CM?.congruence m
(xsdenote eq m am (sort hi))
(xsdenote eq m am (sort lo))
(xsdenote eq m am hi)
(xsdenote eq m am lo);
assert (EQ?.eq eq
(xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo));
EQ?.reflexivity eq (select pivot am);
CM?.congruence m
(select pivot am)
(xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))
(select pivot am)
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo);
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)));
partition_equiv eq m am pivot q;
CM?.congruence m
(select pivot am)
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select pivot am)
(xsdenote eq m am q);
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo))
(select pivot am `CM?.mult m` (xsdenote eq m am q));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am q)));
lemma_xsdenote_aux eq m am pivot q;
EQ?.symmetry eq
(xsdenote eq m am (pivot::q))
(select pivot am `CM?.mult m` (xsdenote eq m am q));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am q))
(xsdenote eq m am xs);
EQ?.symmetry eq (xsdenote eq m am (sort xs)) (xsdenote eq m am xs)
#pop-options
#push-options "--fuel 0 --ifuel 0"
(* Lemmas to be called after a permutation compatible with AC-unification was found *)
let smt_reflexivity (#a:Type) (eq:CE.equiv a) (x y:a)
: Lemma (requires x == y)
(ensures CE.EQ?.eq eq x y)
= CE.EQ?.reflexivity eq x
let identity_left_smt (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x y:a)
: Lemma
(requires x == y)
(ensures CE.EQ?.eq eq x (CE.CM?.mult m (CE.CM?.unit m) y))
= CE.CM?.identity m x;
CE.EQ?.symmetry eq (CE.CM?.mult m (CE.CM?.unit m) x) x
let identity_left (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x:a)
: Lemma (CE.EQ?.eq eq x (CE.CM?.mult m (CE.CM?.unit m) x))
= CE.CM?.identity m x;
CE.EQ?.symmetry eq (CE.CM?.mult m (CE.CM?.unit m) x) x
let identity_right_diff (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x y:a) : Lemma
(requires CE.EQ?.eq eq x y)
(ensures CE.EQ?.eq eq (CE.CM?.mult m x (CE.CM?.unit m)) y)
= CE.right_identity eq m x;
CE.EQ?.transitivity eq (CE.CM?.mult m x (CE.CM?.unit m)) x y
/// Dismiss possible vprops goals that might have been created by lemma application.
/// These vprops will be instantiated at a later stage; else, Meta-F* will raise an error
let rec dismiss_slprops () : Tac unit =
match term_as_formula' (cur_goal ()) with
| App t _ -> if is_fvar t (`%squash) then () else (dismiss(); dismiss_slprops ())
| _ -> dismiss(); dismiss_slprops ()
/// Recursively removing trailing empty assertions
let rec n_identity_left (n:int) (eq m:term) : Tac unit
= if n = 0 then (
apply_lemma (`(CE.EQ?.reflexivity (`#eq)));
// Cleaning up, in case a uvar has been generated here. It'll be solved later
set_goals [])
else (
apply_lemma (`identity_right_diff (`#eq) (`#m));
// Drop the slprops generated, they will be solved later
dismiss_slprops ();
n_identity_left (n-1) eq m
)
/// Helper lemma: If two vprops (as represented by lists of atoms) are equivalent, then their canonical forms
/// (corresponding to applying the sort function on atoms) are equivalent
let equivalent_sorted (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (l1 l2 l1' l2':list atom)
: Lemma (requires
sort l1 == sort l1' /\
sort l2 == sort l2' /\
xsdenote eq m am l1 `CE.EQ?.eq eq` xsdenote eq m am l2)
(ensures xsdenote eq m am l1' `CE.EQ?.eq eq` xsdenote eq m am l2')
= let open FStar.Algebra.CommMonoid.Equiv in
sort_correct_aux eq m am l1';
sort_correct_aux eq m am l1;
EQ?.symmetry eq (xsdenote eq m am l1) (xsdenote eq m am (sort l1));
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am (sort l1'))
(xsdenote eq m am l1);
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am l1)
(xsdenote eq m am l2);
sort_correct_aux eq m am l2;
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am l2)
(xsdenote eq m am (sort l2));
sort_correct_aux eq m am l2';
EQ?.symmetry eq (xsdenote eq m am l2') (xsdenote eq m am (sort l2'));
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am (sort l2))
(xsdenote eq m am l2')
#pop-options
/// Finds the position of first occurrence of x in xs.
/// This is now specialized to terms and their funny term_eq_old.
let rec where_aux (n:nat) (x:term) (xs:list term) :
Tac (option nat) (decreases xs) =
match xs with
| [] -> None
| x'::xs' -> if term_eq_old x x' then Some n else where_aux (n+1) x xs'
let where = where_aux 0
let fatom (t:term) (ts:list term) (am:amap term) : Tac (exp * list term * amap term) =
match where t ts with
| Some v -> (Atom v, ts, am)
| None ->
let vfresh = List.Tot.Base.length ts in
let t = norm_term [iota; zeta] t in
(Atom vfresh, ts `List.Tot.append` [t], update vfresh t am)
/// Transforming a term into the corresponding list of atoms
/// If the atomic terms were already present in the map [am], then
/// they correspond to the same atoms
/// This expects that mult, unit, and t have already been normalized
let rec reification_aux (ts:list term) (am:amap term)
(mult unit t : term) : Tac (exp * list term * amap term) =
let hd, tl = collect_app_ref t in
match inspect_unascribe hd, List.Tot.Base.list_unref tl with
| Tv_FVar fv, [(t1, Q_Explicit) ; (t2, Q_Explicit)] ->
if term_eq_old (pack (Tv_FVar fv)) mult
then (let (e1, ts, am) = reification_aux ts am mult unit t1 in
let (e2, ts, am) = reification_aux ts am mult unit t2 in
(Mult e1 e2, ts, am))
else fatom t ts am
| _, _ ->
if term_eq_old t unit
then (Unit, ts, am)
else fatom t ts am
/// Performs the required normalization before calling the function above
let reification (eq: term) (m: term) (ts:list term) (am:amap term) (t:term) :
Tac (exp * list term * amap term) =
let mult = norm_term [iota; zeta; delta] (`CE.CM?.mult (`#m)) in
let unit = norm_term [iota; zeta; delta] (`CE.CM?.unit (`#m)) in
let t = norm_term [iota; zeta] t in
reification_aux ts am mult unit t
/// Meta-F* internal: Transforms the atom map into a term
let rec convert_map (m : list (atom * term)) : term =
match m with
| [] -> `[]
| (a, t)::ps ->
let a = pack (Tv_Const (C_Int a)) in
(* let t = norm_term [delta] t in *)
`((`#a, (`#t)) :: (`#(convert_map ps)))
/// `am` is an amap (basically a list) of terms, each representing a value
/// of type `a` (whichever we are canonicalizing). This functions converts
/// `am` into a single `term` of type `amap a`, suitable to call `mdenote` with *)
let convert_am (am : amap term) : term =
let (map, def) = am in
(* let def = norm_term [delta] def in *)
`( (`#(convert_map map), `#def) )
/// Transforms a term representatoin into a term through quotation
let rec quote_exp (e:exp) : term =
match e with
| Unit -> (`Unit)
| Mult e1 e2 -> (`Mult (`#(quote_exp e1)) (`#(quote_exp e2)))
| Atom n -> let nt = pack (Tv_Const (C_Int n)) in
(`Atom (`#nt))
let rec quote_atoms (l:list atom) = match l with
| [] -> `[]
| hd::tl -> let nt = pack (Tv_Const (C_Int hd)) in
(`Cons (`#nt) (`#(quote_atoms tl)))
/// Some internal normalization steps to make reflection of vprops into atoms and atom permutation go smoothly. | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Reflection.V2.Derived.Lemmas",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | Prims.list FStar.Pervasives.norm_step | Prims.Tot | [
"total"
] | [] | [
"Prims.Cons",
"FStar.Pervasives.norm_step",
"FStar.Pervasives.primops",
"FStar.Pervasives.iota",
"FStar.Pervasives.zeta",
"FStar.Pervasives.delta_only",
"Prims.string",
"Prims.Nil"
] | [] | false | false | false | true | false | let normal_tac_steps =
| [
primops;
iota;
zeta;
delta_only [
`%mdenote; `%select; `%my_assoc; `%my_append; `%flatten; `%sort; `%my_sortWith; `%my_partition;
`%my_bool_of_compare; `%my_compare_of_bool; `%fst; `%__proj__Mktuple2__item___1; `%snd;
`%__proj__Mktuple2__item___2; `%CE.__proj__CM__item__unit; `%CE.__proj__CM__item__mult; `%rm
]
] | false |
|
Steel.Effect.Common.fsti | Steel.Effect.Common.new_args_for_smt_attrs | val new_args_for_smt_attrs (env: env) (l: list argv) (ty: typ) : Tac (list argv * list term) | val new_args_for_smt_attrs (env: env) (l: list argv) (ty: typ) : Tac (list argv * list term) | let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?" | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 66,
"end_line": 967,
"start_col": 0,
"start_line": 936
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
env: FStar.Stubs.Reflection.Types.env ->
l: Prims.list FStar.Stubs.Reflection.V2.Data.argv ->
ty: FStar.Stubs.Reflection.Types.typ
-> FStar.Tactics.Effect.Tac
(Prims.list FStar.Stubs.Reflection.V2.Data.argv * Prims.list FStar.Tactics.NamedView.term) | FStar.Tactics.Effect.Tac | [] | [] | [
"FStar.Stubs.Reflection.Types.env",
"Prims.list",
"FStar.Stubs.Reflection.V2.Data.argv",
"FStar.Stubs.Reflection.Types.typ",
"FStar.Stubs.Reflection.Types.term",
"FStar.Stubs.Reflection.V2.Data.aqualv",
"FStar.Pervasives.Native.tuple2",
"FStar.Tactics.NamedView.binder",
"FStar.Tactics.NamedView.comp",
"FStar.Tactics.NamedView.term",
"FStar.Pervasives.Native.Mktuple2",
"Prims.Cons",
"Prims.bool",
"Steel.Effect.Common.new_args_for_smt_attrs",
"FStar.Tactics.NamedView.inspect_comp",
"FStar.Stubs.Reflection.V2.Data.universes",
"FStar.Stubs.Reflection.Types.name",
"Prims.op_Equality",
"Prims.string",
"Prims.Nil",
"FStar.Tactics.V2.Derived.fail",
"FStar.Stubs.Reflection.V2.Data.comp_view",
"Prims.unit",
"FStar.Tactics.V2.Derived.flip",
"FStar.Stubs.Tactics.V2.Builtins.unshelve",
"FStar.Stubs.Tactics.V2.Builtins.tc",
"Steel.Effect.Common.is_smt_binder",
"FStar.Stubs.Reflection.Types.fv",
"FStar.Tactics.NamedView.named_term_view",
"FStar.Tactics.V2.SyntaxHelpers.inspect_unascribe",
"FStar.Tactics.NamedView.term_view",
"Prims.b2t",
"FStar.Tactics.NamedView.notAscription",
"FStar.Stubs.Tactics.V2.Builtins.ghost_uvar_env",
"FStar.Tactics.V2.Derived.cur_env"
] | [
"recursion"
] | false | true | false | false | false | let rec new_args_for_smt_attrs (env: env) (l: list argv) (ty: typ) : Tac (list argv * list term) =
| let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv) :: tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt
then
(let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv))
else (arg, aqualv)
in
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops"
in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd :: tl_argv, (if needs_smt then arg :: tl_terms else tl_terms)
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?" | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.equivalent_lists_once_l2 | val equivalent_lists_once_l2 (l1 l2 l1_del l2_del: list atom) (am: amap term)
: Tac (list atom * list atom * list atom * list atom) | val equivalent_lists_once_l2 (l1 l2 l1_del l2_del: list atom) (am: amap term)
: Tac (list atom * list atom * list atom * list atom) | let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 5,
"end_line": 907,
"start_col": 0,
"start_line": 888
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
l1: Prims.list Steel.Effect.Common.atom ->
l2: Prims.list Steel.Effect.Common.atom ->
l1_del: Prims.list Steel.Effect.Common.atom ->
l2_del: Prims.list Steel.Effect.Common.atom ->
am: Steel.Effect.Common.amap FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac
(((Prims.list Steel.Effect.Common.atom * Prims.list Steel.Effect.Common.atom) *
Prims.list Steel.Effect.Common.atom) *
Prims.list Steel.Effect.Common.atom) | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.list",
"Steel.Effect.Common.atom",
"Steel.Effect.Common.amap",
"FStar.Tactics.NamedView.term",
"FStar.Pervasives.Native.Mktuple4",
"Prims.Nil",
"FStar.Pervasives.Native.tuple4",
"Prims.Cons",
"Steel.Effect.Common.equivalent_lists_once_l2",
"Prims.bool",
"Prims.int",
"Prims.op_Equality",
"Steel.Effect.Common.remove_from_list",
"FStar.Pervasives.Native.tuple2",
"Steel.Effect.Common.try_candidates",
"Steel.Effect.Common.is_uvar",
"Steel.Effect.Common.select"
] | [
"recursion"
] | false | true | false | false | false | let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del: list atom) (am: amap term)
: Tac (list atom * list atom * list atom * list atom) =
| match l2 with
| [] -> l1, [], l1_del, l2_del
| hd :: tl ->
if is_uvar (select hd am)
then
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd :: rem2, l1'_del, l2'_del
else
(let t, n = try_candidates hd l1 am in
if n = 1
then
(let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t :: l1_del) (hd :: l2_del) am)
else
(let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd :: rem2, l1'_del, l2'_del)) | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.flatten | val flatten (e: exp) : list atom | val flatten (e: exp) : list atom | let rec flatten (e:exp) : list atom =
match e with
| Unit -> []
| Atom x -> [x]
| Mult e1 e2 -> flatten e1 `my_append` flatten e2 | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 51,
"end_line": 1152,
"start_col": 0,
"start_line": 1148
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am
/// Iterates over all terms in [l2] to prepare them for unification with SMT rewriting
let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2
/// Recursively calls equivalent_lists_once.
/// Stops when we're done with unification, or when we didn't make any progress
/// If we didn't make any progress, we have too many candidates for some terms.
/// Accumulates rewritings of l1 and l2 in l1_del and l2_del, with the invariant
/// that the two lists are unifiable at any point
/// The boolean indicates if there is a leftover empty frame
let rec equivalent_lists' (n:nat) (use_smt:bool) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, []
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Try to make progress by matching non-uvars of l2 with candidates in l1
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Should always be smaller or equal to n
// If it is equal, no progress was made.
if use_smt then
// SMT fallback is allowed
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
/// Checks if term for atom t unifies with fall uvars in l
let rec unifies_with_all_uvars (t:term) (l:list atom) (am:amap term) : Tac bool =
match l with
| [] -> true
| hd::tl ->
if unifies_with_all_uvars t tl am then (
// Unified with tail, try this term
let hd_t = select hd am in
if is_uvar hd_t then (
// The head term is a uvar, try unifying
try if unify t hd_t then raise Success else raise Failed
with | Success -> true | _ -> false
) else true // The uvar is not a head term, we do not need to try it
) else false
/// Puts all terms in l1 that cannot unify with the uvars in l2 at the top:
/// They need to be solved first
let rec most_restricted_at_top (l1 l2:list atom) (am:amap term) : Tac (list atom) =
match l1 with
| [] -> []
| hd::tl ->
if unifies_with_all_uvars (select hd am) l2 am then (most_restricted_at_top tl l2 am) `List.Tot.append` [hd]
else hd::(most_restricted_at_top tl l2 am)
/// Core AC-unification tactic.
/// First remove all trivially equal terms, then try to decide equivalence.
/// Assumes that l1 does not contain any vprop uvar.
/// If it succeeds, returns permutations of l1, l2, and a boolean indicating
/// if l2 has a trailing empty frame to be unified
let equivalent_lists (use_smt:bool) (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term)
= let l1, l2, l1_del, l2_del = trivial_cancels l1 l2 am in
let l1 = most_restricted_at_top l1 l2 am in
let n = List.Tot.length l1 in
let l1_del, l2_del, emp_frame, uvar_terms = equivalent_lists' n use_smt l1 l2 l1_del l2_del am in
l1_del, l2_del, emp_frame, uvar_terms
(* Helpers to relate the actual terms to their representation as a list of atoms *)
open FStar.Reflection.V2.Derived.Lemmas
let rec list_to_string (l:list term) : Tac string =
match l with
| [] -> "end"
| hd::tl -> term_to_string hd ^ " " ^ list_to_string tl
let rec mdenote_gen (#a:Type u#aa) (unit:a) (mult:a -> a -> a) (am:amap a) (e:exp) : a =
match e with
| Unit -> unit
| Atom x -> select x am
| Mult e1 e2 -> mult (mdenote_gen unit mult am e1) (mdenote_gen unit mult am e2)
let rec xsdenote_gen (#a:Type) (unit:a) (mult:a -> a -> a) (am:amap a) (xs:list atom) : a =
match xs with
| [] -> unit
| [x] -> select x am
| x::xs' -> mult (select x am) (xsdenote_gen unit mult am xs')
unfold
let mdenote (#a:Type u#aa) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp) : a =
let open FStar.Algebra.CommMonoid.Equiv in
mdenote_gen (CM?.unit m) (CM?.mult m) am e
unfold
let xsdenote (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom) : a =
let open FStar.Algebra.CommMonoid.Equiv in
xsdenote_gen (CM?.unit m) (CM?.mult m) am xs
// We reimplement List.Tot.Base.append because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_append (#t: Type) (l1 l2: list t) : Pure (list t)
(requires True)
(ensures (fun res -> res == l1 `List.Tot.append` l2))
(decreases l1)
= match l1 with
| [] -> l2
| a :: q -> a :: my_append q l2 | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Reflection.V2.Derived.Lemmas",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | e: Steel.Effect.Common.exp -> Prims.list Steel.Effect.Common.atom | Prims.Tot | [
"total"
] | [] | [
"Steel.Effect.Common.exp",
"Prims.Nil",
"Steel.Effect.Common.atom",
"Prims.Cons",
"Steel.Effect.Common.my_append",
"Steel.Effect.Common.flatten",
"Prims.list"
] | [
"recursion"
] | false | false | false | true | false | let rec flatten (e: exp) : list atom =
| match e with
| Unit -> []
| Atom x -> [x]
| Mult e1 e2 -> (flatten e1) `my_append` (flatten e2) | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.most_restricted_at_top | val most_restricted_at_top (l1 l2: list atom) (am: amap term) : Tac (list atom) | val most_restricted_at_top (l1 l2: list atom) (am: amap term) : Tac (list atom) | let rec most_restricted_at_top (l1 l2:list atom) (am:amap term) : Tac (list atom) =
match l1 with
| [] -> []
| hd::tl ->
if unifies_with_all_uvars (select hd am) l2 am then (most_restricted_at_top tl l2 am) `List.Tot.append` [hd]
else hd::(most_restricted_at_top tl l2 am) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 46,
"end_line": 1091,
"start_col": 0,
"start_line": 1086
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am
/// Iterates over all terms in [l2] to prepare them for unification with SMT rewriting
let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2
/// Recursively calls equivalent_lists_once.
/// Stops when we're done with unification, or when we didn't make any progress
/// If we didn't make any progress, we have too many candidates for some terms.
/// Accumulates rewritings of l1 and l2 in l1_del and l2_del, with the invariant
/// that the two lists are unifiable at any point
/// The boolean indicates if there is a leftover empty frame
let rec equivalent_lists' (n:nat) (use_smt:bool) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, []
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Try to make progress by matching non-uvars of l2 with candidates in l1
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Should always be smaller or equal to n
// If it is equal, no progress was made.
if use_smt then
// SMT fallback is allowed
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
/// Checks if term for atom t unifies with fall uvars in l
let rec unifies_with_all_uvars (t:term) (l:list atom) (am:amap term) : Tac bool =
match l with
| [] -> true
| hd::tl ->
if unifies_with_all_uvars t tl am then (
// Unified with tail, try this term
let hd_t = select hd am in
if is_uvar hd_t then (
// The head term is a uvar, try unifying
try if unify t hd_t then raise Success else raise Failed
with | Success -> true | _ -> false
) else true // The uvar is not a head term, we do not need to try it
) else false
/// Puts all terms in l1 that cannot unify with the uvars in l2 at the top: | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
l1: Prims.list Steel.Effect.Common.atom ->
l2: Prims.list Steel.Effect.Common.atom ->
am: Steel.Effect.Common.amap FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac (Prims.list Steel.Effect.Common.atom) | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.list",
"Steel.Effect.Common.atom",
"Steel.Effect.Common.amap",
"FStar.Tactics.NamedView.term",
"Prims.Nil",
"FStar.List.Tot.Base.append",
"Prims.Cons",
"Steel.Effect.Common.most_restricted_at_top",
"Prims.bool",
"Steel.Effect.Common.unifies_with_all_uvars",
"Steel.Effect.Common.select"
] | [
"recursion"
] | false | true | false | false | false | let rec most_restricted_at_top (l1 l2: list atom) (am: amap term) : Tac (list atom) =
| match l1 with
| [] -> []
| hd :: tl ->
if unifies_with_all_uvars (select hd am) l2 am
then (most_restricted_at_top tl l2 am) `List.Tot.append` [hd]
else hd :: (most_restricted_at_top tl l2 am) | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.try_unifying_remaining | val try_unifying_remaining (l: list atom) (u: term) (am: amap term) : Tac unit | val try_unifying_remaining (l: list atom) (u: term) (am: amap term) : Tac unit | let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am)) | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 93,
"end_line": 927,
"start_col": 0,
"start_line": 921
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it. | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false |
l: Prims.list Steel.Effect.Common.atom ->
u578: FStar.Tactics.NamedView.term ->
am: Steel.Effect.Common.amap FStar.Tactics.NamedView.term
-> FStar.Tactics.Effect.Tac Prims.unit | FStar.Tactics.Effect.Tac | [] | [] | [
"Prims.list",
"Steel.Effect.Common.atom",
"FStar.Tactics.NamedView.term",
"Steel.Effect.Common.amap",
"Prims.unit",
"FStar.Tactics.V2.Derived.try_with",
"FStar.Tactics.Effect.raise",
"Steel.Effect.Common.Success",
"Prims.bool",
"Steel.Effect.Common.Failed",
"FStar.Tactics.V2.Derived.unify",
"Steel.Effect.Common.select",
"Prims.exn",
"Steel.Effect.Common.try_unifying_remaining",
"FStar.Tactics.V2.Derived.fail",
"Prims.string",
"Prims.op_Hat",
"FStar.Stubs.Tactics.V2.Builtins.term_to_string"
] | [
"recursion"
] | false | true | false | false | false | let rec try_unifying_remaining (l: list atom) (u: term) (am: amap term) : Tac unit =
| match l with
| [] -> ()
| hd :: tl ->
try if unify u (select hd am) then raise Success else raise Failed
with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am)) | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.close_equality_typ' | val close_equality_typ' (t: term) : Tac unit | val close_equality_typ' (t: term) : Tac unit | let close_equality_typ' (t:term) : Tac unit =
let f = term_as_formula' t in
match f with
| Comp (Eq (Some u)) l _ -> if is_uvar u then (unshelve u; exact_with_ref (tc (cur_env()) l))
| _ -> () | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 11,
"end_line": 1712,
"start_col": 0,
"start_line": 1708
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am
/// Iterates over all terms in [l2] to prepare them for unification with SMT rewriting
let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2
/// Recursively calls equivalent_lists_once.
/// Stops when we're done with unification, or when we didn't make any progress
/// If we didn't make any progress, we have too many candidates for some terms.
/// Accumulates rewritings of l1 and l2 in l1_del and l2_del, with the invariant
/// that the two lists are unifiable at any point
/// The boolean indicates if there is a leftover empty frame
let rec equivalent_lists' (n:nat) (use_smt:bool) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, []
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Try to make progress by matching non-uvars of l2 with candidates in l1
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Should always be smaller or equal to n
// If it is equal, no progress was made.
if use_smt then
// SMT fallback is allowed
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
/// Checks if term for atom t unifies with fall uvars in l
let rec unifies_with_all_uvars (t:term) (l:list atom) (am:amap term) : Tac bool =
match l with
| [] -> true
| hd::tl ->
if unifies_with_all_uvars t tl am then (
// Unified with tail, try this term
let hd_t = select hd am in
if is_uvar hd_t then (
// The head term is a uvar, try unifying
try if unify t hd_t then raise Success else raise Failed
with | Success -> true | _ -> false
) else true // The uvar is not a head term, we do not need to try it
) else false
/// Puts all terms in l1 that cannot unify with the uvars in l2 at the top:
/// They need to be solved first
let rec most_restricted_at_top (l1 l2:list atom) (am:amap term) : Tac (list atom) =
match l1 with
| [] -> []
| hd::tl ->
if unifies_with_all_uvars (select hd am) l2 am then (most_restricted_at_top tl l2 am) `List.Tot.append` [hd]
else hd::(most_restricted_at_top tl l2 am)
/// Core AC-unification tactic.
/// First remove all trivially equal terms, then try to decide equivalence.
/// Assumes that l1 does not contain any vprop uvar.
/// If it succeeds, returns permutations of l1, l2, and a boolean indicating
/// if l2 has a trailing empty frame to be unified
let equivalent_lists (use_smt:bool) (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term)
= let l1, l2, l1_del, l2_del = trivial_cancels l1 l2 am in
let l1 = most_restricted_at_top l1 l2 am in
let n = List.Tot.length l1 in
let l1_del, l2_del, emp_frame, uvar_terms = equivalent_lists' n use_smt l1 l2 l1_del l2_del am in
l1_del, l2_del, emp_frame, uvar_terms
(* Helpers to relate the actual terms to their representation as a list of atoms *)
open FStar.Reflection.V2.Derived.Lemmas
let rec list_to_string (l:list term) : Tac string =
match l with
| [] -> "end"
| hd::tl -> term_to_string hd ^ " " ^ list_to_string tl
let rec mdenote_gen (#a:Type u#aa) (unit:a) (mult:a -> a -> a) (am:amap a) (e:exp) : a =
match e with
| Unit -> unit
| Atom x -> select x am
| Mult e1 e2 -> mult (mdenote_gen unit mult am e1) (mdenote_gen unit mult am e2)
let rec xsdenote_gen (#a:Type) (unit:a) (mult:a -> a -> a) (am:amap a) (xs:list atom) : a =
match xs with
| [] -> unit
| [x] -> select x am
| x::xs' -> mult (select x am) (xsdenote_gen unit mult am xs')
unfold
let mdenote (#a:Type u#aa) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp) : a =
let open FStar.Algebra.CommMonoid.Equiv in
mdenote_gen (CM?.unit m) (CM?.mult m) am e
unfold
let xsdenote (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom) : a =
let open FStar.Algebra.CommMonoid.Equiv in
xsdenote_gen (CM?.unit m) (CM?.mult m) am xs
// We reimplement List.Tot.Base.append because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_append (#t: Type) (l1 l2: list t) : Pure (list t)
(requires True)
(ensures (fun res -> res == l1 `List.Tot.append` l2))
(decreases l1)
= match l1 with
| [] -> l2
| a :: q -> a :: my_append q l2
let rec flatten (e:exp) : list atom =
match e with
| Unit -> []
| Atom x -> [x]
| Mult e1 e2 -> flatten e1 `my_append` flatten e2
let rec flatten_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs1 xs2:list atom)
: Lemma (xsdenote eq m am (xs1 `my_append` xs2) `CE.EQ?.eq eq` CE.CM?.mult m (xsdenote eq m am xs1)
(xsdenote eq m am xs2)) =
let open FStar.Algebra.CommMonoid.Equiv in
match xs1 with
| [] ->
CM?.identity m (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.unit m) (xsdenote eq m am xs2)) (xsdenote eq m am xs2)
| [x] -> (
if (Nil? xs2)
then (right_identity eq m (select x am);
EQ?.symmetry eq (CM?.mult m (select x am) (CM?.unit m)) (select x am))
else EQ?.reflexivity eq (CM?.mult m (xsdenote eq m am [x]) (xsdenote eq m am xs2)))
| x::xs1' ->
flatten_correct_aux eq m am xs1' xs2;
EQ?.reflexivity eq (select x am);
CM?.congruence m (select x am) (xsdenote eq m am (xs1' `my_append` xs2))
(select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2));
CM?.associativity m (select x am) (xsdenote eq m am xs1') (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)));
EQ?.transitivity eq (CM?.mult m (select x am) (xsdenote eq m am (xs1' `my_append` xs2)))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)))
(CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
let rec flatten_correct (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp)
: Lemma (mdenote eq m am e `CE.EQ?.eq eq` xsdenote eq m am (flatten e)) =
let open FStar.Algebra.CommMonoid.Equiv in
match e with
| Unit -> EQ?.reflexivity eq (CM?.unit m)
| Atom x -> EQ?.reflexivity eq (select x am)
| Mult e1 e2 ->
flatten_correct_aux eq m am (flatten e1) (flatten e2);
EQ?.symmetry eq (xsdenote eq m am (flatten e1 `my_append` flatten e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)));
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CM?.congruence m (mdenote eq m am e1) (mdenote eq m am e2)
(xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2));
EQ?.transitivity eq (CM?.mult m (mdenote eq m am e1) (mdenote eq m am e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)))
(xsdenote eq m am (flatten e1 `my_append` flatten e2))
let monoid_reflect (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e1 e2:exp)
(_ : squash (xsdenote eq m am (flatten e1) `CE.EQ?.eq eq` xsdenote eq m am (flatten e2)))
: squash (mdenote eq m am e1 `CE.EQ?.eq eq` mdenote eq m am e2) =
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CE.EQ?.symmetry eq (mdenote eq m am e2) (xsdenote eq m am (flatten e2));
CE.EQ?.transitivity eq
(xsdenote eq m am (flatten e1))
(xsdenote eq m am (flatten e2))
(mdenote eq m am e2);
CE.EQ?.transitivity eq
(mdenote eq m am e1)
(xsdenote eq m am (flatten e1))
(mdenote eq m am e2)
// Here we sort the variable numbers
// We reimplement List.Tot.Base.sortWith because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_partition (#a: Type) (f: (a -> Tot bool)) (l: list a)
: Pure (list a & list a)
(requires True)
(ensures (fun res -> res == List.Tot.partition f l))
= match l with
| [] -> [], []
| hd::tl ->
let l1, l2 = my_partition f tl in
if f hd
then hd::l1, l2
else l1, hd::l2
let rec partition_ext (#a: Type) (f1 f2: (a -> Tot bool)) (l: list a)
: Lemma
(requires (forall x . f1 x == f2 x))
(ensures (List.Tot.partition f1 l == List.Tot.partition f2 l))
= match l with
| [] -> ()
| hd::tl -> partition_ext f1 f2 tl
let my_bool_of_compare (#a: Type) (f: a -> a -> Tot int) (x: a) (y: a) : Tot bool
= f x y < 0
let rec my_sortWith (#a: Type) (f: (a -> a -> Tot int)) (l:list a)
: Pure (list a)
(requires True)
(ensures (fun res -> res == List.Tot.sortWith f l))
(decreases (List.Tot.length l))
= match l with
| [] -> []
| pivot::tl ->
let hi, lo = my_partition (my_bool_of_compare f pivot) tl in
partition_ext (my_bool_of_compare f pivot) (List.Tot.bool_of_compare f pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f pivot) tl;
my_append (my_sortWith f lo) (pivot::my_sortWith f hi)
let rec sortWith_ext (#a: Type) (f1 f2: (a -> a -> Tot int)) (l: list a)
: Lemma
(requires (forall x y . f1 x y == f2 x y))
(ensures (List.Tot.sortWith f1 l == List.Tot.sortWith f2 l))
(decreases (List.Tot.length l))
= match l with
| [] -> ()
| pivot::tl ->
partition_ext (List.Tot.bool_of_compare f1 pivot) (List.Tot.bool_of_compare f2 pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f1 pivot) tl;
let hi, lo = List.Tot.partition (List.Tot.bool_of_compare f1 pivot) tl in
sortWith_ext f1 f2 lo;
sortWith_ext f1 f2 hi
let permute = list atom -> list atom
let my_compare_of_bool (#a:eqtype) (rel: a -> a -> Tot bool) (x: a) (y: a) : Tot int
= if x `rel` y then -1
else if x = y then 0
else 1
let sort : permute = my_sortWith #int (my_compare_of_bool (<))
#push-options "--fuel 1 --ifuel 1"
let lemma_xsdenote_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (hd:atom) (tl:list atom)
: Lemma (xsdenote eq m am (hd::tl) `CE.EQ?.eq eq`
(CE.CM?.mult m (select hd am) (xsdenote eq m am tl)))
= let open FStar.Algebra.CommMonoid.Equiv in
match tl with
| [] ->
assert (xsdenote eq m am (hd::tl) == select hd am);
CM?.identity m (select hd am);
EQ?.symmetry eq (CM?.unit m `CM?.mult m` select hd am) (select hd am);
CM?.commutativity m (CM?.unit m) (select hd am);
EQ?.transitivity eq
(xsdenote eq m am (hd::tl))
(CM?.unit m `CM?.mult m` select hd am)
(CM?.mult m (select hd am) (xsdenote eq m am tl))
| _ -> EQ?.reflexivity eq (xsdenote eq m am (hd::tl))
let rec partition_equiv (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (pivot:atom) (q:list atom)
: Lemma
(let open FStar.List.Tot.Base in
let hi, lo = partition (bool_of_compare (compare_of_bool (<)) pivot) q in
CE.EQ?.eq eq
(xsdenote eq m am hi `CE.CM?.mult m` xsdenote eq m am lo)
(xsdenote eq m am q))
= let open FStar.Algebra.CommMonoid.Equiv in
let open FStar.List.Tot.Base in
let f = bool_of_compare (compare_of_bool (<)) pivot in
let hi, lo = partition f q in
match q with
| [] -> CM?.identity m (xsdenote eq m am hi)
| hd::tl ->
let l1, l2 = partition f tl in
partition_equiv eq m am pivot tl;
assert (EQ?.eq eq
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(xsdenote eq m am tl));
EQ?.reflexivity eq (xsdenote eq m am l1);
EQ?.reflexivity eq (xsdenote eq m am l2);
EQ?.reflexivity eq (xsdenote eq m am hi);
EQ?.reflexivity eq (xsdenote eq m am lo);
if f hd then begin
assert (hi == hd::l1 /\ lo == l2);
lemma_xsdenote_aux eq m am hd l1;
CM?.congruence m
(xsdenote eq m am hi)
(xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am l1)
(xsdenote eq m am l2);
CM?.associativity m
(select hd am)
(xsdenote eq m am l1)
(xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
((select hd am `CM?.mult m` xsdenote eq m am l1) `CM?.mult m` xsdenote eq m am l2)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2));
EQ?.reflexivity eq (select hd am);
CM?.congruence m
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(select hd am)
(xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2))
(select hd am `CM?.mult m` xsdenote eq m am tl);
lemma_xsdenote_aux eq m am hd tl;
EQ?.symmetry eq
(xsdenote eq m am (hd::tl))
(select hd am `CM?.mult m` xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am tl)
(xsdenote eq m am (hd::tl))
end else begin
assert (hi == l1 /\ lo == hd::l2);
lemma_xsdenote_aux eq m am hd l2;
CM?.congruence m
(xsdenote eq m am hi)
(xsdenote eq m am lo)
(xsdenote eq m am l1)
(select hd am `CM?.mult m` xsdenote eq m am l2);
CM?.commutativity m
(xsdenote eq m am l1)
(select hd am `CM?.mult m` xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(xsdenote eq m am l1 `CM?.mult m` (select hd am `CM?.mult m` xsdenote eq m am l2))
((select hd am `CM?.mult m` xsdenote eq m am l2) `CM?.mult m` xsdenote eq m am l1);
CM?.associativity m
(select hd am)
(xsdenote eq m am l2)
(xsdenote eq m am l1);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
((select hd am `CM?.mult m` xsdenote eq m am l2) `CM?.mult m` xsdenote eq m am l1)
(select hd am `CM?.mult m` (xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1));
CM?.commutativity m (xsdenote eq m am l2) (xsdenote eq m am l1);
EQ?.reflexivity eq (select hd am);
CM?.congruence m
(select hd am)
(xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1)
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1))
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2));
CM?.congruence m
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(select hd am)
(xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2))
(select hd am `CM?.mult m` xsdenote eq m am tl);
lemma_xsdenote_aux eq m am hd tl;
EQ?.symmetry eq
(xsdenote eq m am (hd::tl))
(select hd am `CM?.mult m` xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am tl)
(xsdenote eq m am (hd::tl))
end
let rec sort_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom)
: Lemma (requires True)
(ensures xsdenote eq m am xs `CE.EQ?.eq eq` xsdenote eq m am (sort xs))
(decreases (FStar.List.Tot.Base.length xs))
= let open FStar.Algebra.CommMonoid.Equiv in
match xs with
| [] -> EQ?.reflexivity eq (xsdenote eq m am [])
| pivot::q ->
let sort0 : permute = List.Tot.sortWith #int (List.Tot.compare_of_bool (<)) in
let sort_eq (l: list atom) : Lemma
(sort l == sort0 l)
[SMTPat (sort l)]
= sortWith_ext (my_compare_of_bool (<)) (List.Tot.compare_of_bool (<)) l
in
let open FStar.List.Tot.Base in
let f:int -> int -> int = compare_of_bool (<) in
let hi, lo = partition (bool_of_compare f pivot) q in
flatten_correct_aux eq m am (sort lo) (pivot::sort hi);
assert (xsdenote eq m am (sort xs) `EQ?.eq eq`
CM?.mult m (xsdenote eq m am (sort lo))
(xsdenote eq m am (pivot::sort hi)));
lemma_xsdenote_aux eq m am pivot (sort hi);
EQ?.reflexivity eq (xsdenote eq m am (sort lo));
CM?.congruence m
(xsdenote eq m am (sort lo))
(xsdenote eq m am (pivot::sort hi))
(xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` xsdenote eq m am (sort hi));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` xsdenote eq m am (pivot::sort hi))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi))));
CM?.commutativity m
(xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` xsdenote eq m am (sort hi));
CM?.associativity m
(select pivot am)
(xsdenote eq m am (sort hi))
(xsdenote eq m am (sort lo));
EQ?.transitivity eq
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)))
((select pivot am `CM?.mult m` xsdenote eq m am (sort hi)) `CM?.mult m` xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))));
partition_length (bool_of_compare f pivot) q;
sort_correct_aux eq m am hi;
sort_correct_aux eq m am lo;
EQ?.symmetry eq (xsdenote eq m am lo) (xsdenote eq m am (sort lo));
EQ?.symmetry eq (xsdenote eq m am hi) (xsdenote eq m am (sort hi));
CM?.congruence m
(xsdenote eq m am (sort hi))
(xsdenote eq m am (sort lo))
(xsdenote eq m am hi)
(xsdenote eq m am lo);
assert (EQ?.eq eq
(xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo));
EQ?.reflexivity eq (select pivot am);
CM?.congruence m
(select pivot am)
(xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))
(select pivot am)
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo);
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)));
partition_equiv eq m am pivot q;
CM?.congruence m
(select pivot am)
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select pivot am)
(xsdenote eq m am q);
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo))
(select pivot am `CM?.mult m` (xsdenote eq m am q));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am q)));
lemma_xsdenote_aux eq m am pivot q;
EQ?.symmetry eq
(xsdenote eq m am (pivot::q))
(select pivot am `CM?.mult m` (xsdenote eq m am q));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am q))
(xsdenote eq m am xs);
EQ?.symmetry eq (xsdenote eq m am (sort xs)) (xsdenote eq m am xs)
#pop-options
#push-options "--fuel 0 --ifuel 0"
(* Lemmas to be called after a permutation compatible with AC-unification was found *)
let smt_reflexivity (#a:Type) (eq:CE.equiv a) (x y:a)
: Lemma (requires x == y)
(ensures CE.EQ?.eq eq x y)
= CE.EQ?.reflexivity eq x
let identity_left_smt (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x y:a)
: Lemma
(requires x == y)
(ensures CE.EQ?.eq eq x (CE.CM?.mult m (CE.CM?.unit m) y))
= CE.CM?.identity m x;
CE.EQ?.symmetry eq (CE.CM?.mult m (CE.CM?.unit m) x) x
let identity_left (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x:a)
: Lemma (CE.EQ?.eq eq x (CE.CM?.mult m (CE.CM?.unit m) x))
= CE.CM?.identity m x;
CE.EQ?.symmetry eq (CE.CM?.mult m (CE.CM?.unit m) x) x
let identity_right_diff (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x y:a) : Lemma
(requires CE.EQ?.eq eq x y)
(ensures CE.EQ?.eq eq (CE.CM?.mult m x (CE.CM?.unit m)) y)
= CE.right_identity eq m x;
CE.EQ?.transitivity eq (CE.CM?.mult m x (CE.CM?.unit m)) x y
/// Dismiss possible vprops goals that might have been created by lemma application.
/// These vprops will be instantiated at a later stage; else, Meta-F* will raise an error
let rec dismiss_slprops () : Tac unit =
match term_as_formula' (cur_goal ()) with
| App t _ -> if is_fvar t (`%squash) then () else (dismiss(); dismiss_slprops ())
| _ -> dismiss(); dismiss_slprops ()
/// Recursively removing trailing empty assertions
let rec n_identity_left (n:int) (eq m:term) : Tac unit
= if n = 0 then (
apply_lemma (`(CE.EQ?.reflexivity (`#eq)));
// Cleaning up, in case a uvar has been generated here. It'll be solved later
set_goals [])
else (
apply_lemma (`identity_right_diff (`#eq) (`#m));
// Drop the slprops generated, they will be solved later
dismiss_slprops ();
n_identity_left (n-1) eq m
)
/// Helper lemma: If two vprops (as represented by lists of atoms) are equivalent, then their canonical forms
/// (corresponding to applying the sort function on atoms) are equivalent
let equivalent_sorted (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (l1 l2 l1' l2':list atom)
: Lemma (requires
sort l1 == sort l1' /\
sort l2 == sort l2' /\
xsdenote eq m am l1 `CE.EQ?.eq eq` xsdenote eq m am l2)
(ensures xsdenote eq m am l1' `CE.EQ?.eq eq` xsdenote eq m am l2')
= let open FStar.Algebra.CommMonoid.Equiv in
sort_correct_aux eq m am l1';
sort_correct_aux eq m am l1;
EQ?.symmetry eq (xsdenote eq m am l1) (xsdenote eq m am (sort l1));
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am (sort l1'))
(xsdenote eq m am l1);
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am l1)
(xsdenote eq m am l2);
sort_correct_aux eq m am l2;
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am l2)
(xsdenote eq m am (sort l2));
sort_correct_aux eq m am l2';
EQ?.symmetry eq (xsdenote eq m am l2') (xsdenote eq m am (sort l2'));
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am (sort l2))
(xsdenote eq m am l2')
#pop-options
/// Finds the position of first occurrence of x in xs.
/// This is now specialized to terms and their funny term_eq_old.
let rec where_aux (n:nat) (x:term) (xs:list term) :
Tac (option nat) (decreases xs) =
match xs with
| [] -> None
| x'::xs' -> if term_eq_old x x' then Some n else where_aux (n+1) x xs'
let where = where_aux 0
let fatom (t:term) (ts:list term) (am:amap term) : Tac (exp * list term * amap term) =
match where t ts with
| Some v -> (Atom v, ts, am)
| None ->
let vfresh = List.Tot.Base.length ts in
let t = norm_term [iota; zeta] t in
(Atom vfresh, ts `List.Tot.append` [t], update vfresh t am)
/// Transforming a term into the corresponding list of atoms
/// If the atomic terms were already present in the map [am], then
/// they correspond to the same atoms
/// This expects that mult, unit, and t have already been normalized
let rec reification_aux (ts:list term) (am:amap term)
(mult unit t : term) : Tac (exp * list term * amap term) =
let hd, tl = collect_app_ref t in
match inspect_unascribe hd, List.Tot.Base.list_unref tl with
| Tv_FVar fv, [(t1, Q_Explicit) ; (t2, Q_Explicit)] ->
if term_eq_old (pack (Tv_FVar fv)) mult
then (let (e1, ts, am) = reification_aux ts am mult unit t1 in
let (e2, ts, am) = reification_aux ts am mult unit t2 in
(Mult e1 e2, ts, am))
else fatom t ts am
| _, _ ->
if term_eq_old t unit
then (Unit, ts, am)
else fatom t ts am
/// Performs the required normalization before calling the function above
let reification (eq: term) (m: term) (ts:list term) (am:amap term) (t:term) :
Tac (exp * list term * amap term) =
let mult = norm_term [iota; zeta; delta] (`CE.CM?.mult (`#m)) in
let unit = norm_term [iota; zeta; delta] (`CE.CM?.unit (`#m)) in
let t = norm_term [iota; zeta] t in
reification_aux ts am mult unit t
/// Meta-F* internal: Transforms the atom map into a term
let rec convert_map (m : list (atom * term)) : term =
match m with
| [] -> `[]
| (a, t)::ps ->
let a = pack (Tv_Const (C_Int a)) in
(* let t = norm_term [delta] t in *)
`((`#a, (`#t)) :: (`#(convert_map ps)))
/// `am` is an amap (basically a list) of terms, each representing a value
/// of type `a` (whichever we are canonicalizing). This functions converts
/// `am` into a single `term` of type `amap a`, suitable to call `mdenote` with *)
let convert_am (am : amap term) : term =
let (map, def) = am in
(* let def = norm_term [delta] def in *)
`( (`#(convert_map map), `#def) )
/// Transforms a term representatoin into a term through quotation
let rec quote_exp (e:exp) : term =
match e with
| Unit -> (`Unit)
| Mult e1 e2 -> (`Mult (`#(quote_exp e1)) (`#(quote_exp e2)))
| Atom n -> let nt = pack (Tv_Const (C_Int n)) in
(`Atom (`#nt))
let rec quote_atoms (l:list atom) = match l with
| [] -> `[]
| hd::tl -> let nt = pack (Tv_Const (C_Int hd)) in
(`Cons (`#nt) (`#(quote_atoms tl)))
/// Some internal normalization steps to make reflection of vprops into atoms and atom permutation go smoothly.
/// We reimplemented sorting/list functions to normalize our uses without normalizing those introduced by the user.
let normal_tac_steps = [primops; iota; zeta; delta_only [
`%mdenote; `%select;
`%my_assoc; `%my_append;
`%flatten; `%sort;
`%my_sortWith; `%my_partition;
`%my_bool_of_compare; `%my_compare_of_bool;
`%fst; `%__proj__Mktuple2__item___1;
`%snd; `%__proj__Mktuple2__item___2;
`%CE.__proj__CM__item__unit;
`%CE.__proj__CM__item__mult;
`%rm]]
/// The normalization function, using the above normalization steps
let normal_tac (#a:Type) (x:a) : a = FStar.Pervasives.norm normal_tac_steps x
/// Helper lemma to establish relation between normalized and initial values
let normal_elim (x:Type0) : Lemma
(requires x)
(ensures normal_tac x)
= ()
exception Result of list atom * list atom * bool * list term
/// F* equalities are typed, but the generated type sometimes is a unification variable. | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Reflection.V2.Derived.Lemmas",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | t: FStar.Tactics.NamedView.term -> FStar.Tactics.Effect.Tac Prims.unit | FStar.Tactics.Effect.Tac | [] | [] | [
"FStar.Tactics.NamedView.term",
"FStar.Stubs.Reflection.Types.typ",
"FStar.Tactics.V2.Derived.exact_with_ref",
"Prims.unit",
"FStar.Stubs.Tactics.V2.Builtins.tc",
"FStar.Stubs.Reflection.Types.term",
"FStar.Stubs.Reflection.Types.env",
"FStar.Tactics.V2.Derived.cur_env",
"FStar.Stubs.Tactics.V2.Builtins.unshelve",
"Prims.bool",
"Steel.Effect.Common.is_uvar",
"FStar.Reflection.V2.Formula.formula",
"FStar.Reflection.V2.Formula.term_as_formula'"
] | [] | false | true | false | false | false | let close_equality_typ' (t: term) : Tac unit =
| let f = term_as_formula' t in
match f with
| Comp (Eq (Some u)) l _ ->
if is_uvar u
then
(unshelve u;
exact_with_ref (tc (cur_env ()) l))
| _ -> () | false |
Steel.Effect.Common.fsti | Steel.Effect.Common.bor | val bor : _: Prims.bool -> _: Prims.bool -> Prims.bool | let bor = op_BarBar | {
"file_name": "lib/steel/Steel.Effect.Common.fsti",
"git_rev": "f984200f79bdc452374ae994a5ca837496476c41",
"git_url": "https://github.com/FStarLang/steel.git",
"project_name": "steel"
} | {
"end_col": 19,
"end_line": 1767,
"start_col": 0,
"start_line": 1767
} | (*
Copyright 2020 Microsoft Research
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*)
module Steel.Effect.Common
open Steel.Memory
module Mem = Steel.Memory
module FExt = FStar.FunctionalExtensionality
open FStar.Ghost
/// This module provides various predicates and functions which are common to the
/// different Steel effects.
/// It also contains the tactic responsible for frame inference through a variant of AC-unification
#set-options "--ide_id_info_off"
(* Normalization helpers *)
irreducible let framing_implicit : unit = ()
irreducible let __steel_reduce__ : unit = ()
/// An internal attribute for finer-grained normalization in framing equalities
irreducible let __inner_steel_reduce__ : unit = ()
irreducible let __reduce__ : unit = ()
irreducible let smt_fallback : unit = ()
irreducible let ite_attr : unit = ()
// Needed to avoid some logical vs prop issues during unification with no subtyping
[@@__steel_reduce__]
unfold
let true_p : prop = True
module T = FStar.Tactics.V2
let join_preserves_interp (hp:slprop) (m0:hmem hp) (m1:mem{disjoint m0 m1})
: Lemma
(interp hp (join m0 m1))
[SMTPat (interp hp (join m0 m1))]
= let open Steel.Memory in
intro_emp m1;
intro_star hp emp m0 m1;
affine_star hp emp (join m0 m1)
(* Definition of a selector for a given slprop *)
/// A selector of type `a` for a separation logic predicate hp is a function
/// from a memory where the predicate hp holds, which returns a value of type `a`.
/// The effect GTot indicates that selectors are ghost functions, used for specification
/// and proof purposes only
let selector' (a:Type0) (hp:slprop) = hmem hp -> GTot a
/// Self-framing property for selectors
let sel_depends_only_on (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp) (m1:mem{disjoint m0 m1}).
(interp_depends_only_on hp; (
sel m0 == sel (join m0 m1)))
/// Additional property that selectors must satisfy, related to internals of
/// the Steel memory model encoding
let sel_depends_only_on_core (#a:Type) (#hp:slprop) (sel:selector' a hp) =
forall (m0:hmem hp). sel m0 == sel (core_mem m0)
/// Full definition of a selector, as a function which satisfies the two predicates above
let selector (a:Type) (hp:slprop) : Type =
sel:selector' a hp{sel_depends_only_on sel /\ sel_depends_only_on_core sel}
/// The basis of our selector framework: Separation logic assertions enhanced with selectors
/// Note that selectors are "optional", it is always possible to use a non-informative selector,
/// such as fun _ -> () and to rely on the standard separation logic reasoning
[@@ erasable]
noeq
type vprop' =
{ hp: slprop u#1;
t:Type0;
sel: selector t hp}
(* Lifting the star operator to an inductive type makes normalization
and implementing some later functions easier *)
[@@__steel_reduce__; erasable]
noeq
type vprop =
| VUnit : vprop' -> vprop
| VStar: vprop -> vprop -> vprop
(* A generic lift from slprop to vprop with a non-informative selector *)
[@@ __steel_reduce__]
let to_vprop' (p:slprop) = {hp = p; t = unit; sel = fun _ -> ()}
[@@ __steel_reduce__]
unfold
let to_vprop (p:slprop) = VUnit (to_vprop' p)
/// Normalization steps for norm below.
/// All functions marked as `unfold`, or with the `__steel_reduce__` attribute will be reduced,
/// as well as some functions internal to the selector framework
unfold
let normal_steps =
[delta_attr [`%__steel_reduce__; `%__inner_steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify]
/// The core normalization primitive used to simplify Verification Conditions before encoding
/// them to an SMT solver.
unfold
let normal (#a:Type) (x:a) = norm normal_steps x
/// An abbreviation for the VStar constructor, allowing to use it with infix notation
[@@ __steel_reduce__; __reduce__]
let star = VStar
/// Extracting the underlying separation logic assertion from a vprop
[@@ __steel_reduce__]
let rec hp_of (p:vprop) = match p with
| VUnit p -> p.hp
| VStar p1 p2 -> hp_of p1 `Mem.star` hp_of p2
/// Extracting the selector type from a vprop
[@@ __steel_reduce__]
let rec t_of (p:vprop) = match p with
| VUnit p -> p.t
| VStar p1 p2 -> t_of p1 * t_of p2
/// Extracting the selector from a vprop
[@@ __steel_reduce__]
let rec sel_of (p:vprop) : GTot (selector (t_of p) (hp_of p)) = match p with
| VUnit p -> fun h -> p.sel h
| VStar p1 p2 ->
let sel1 = sel_of p1 in
let sel2 = sel_of p2 in
fun h -> (sel1 h, sel2 h)
/// Type abbreviations for separation logic pre- and postconditions of the Steel effects
type pre_t = vprop
type post_t (a:Type) = a -> vprop
/// An annotation to indicate which separation logic predicates correspond to monadic computations
/// These computations are handled in a specific manner in the framing tactic; they correspond to places where
/// the context shrinks from all local variables in the computation to variables available at the toplevel
let return_pre (p:vprop) : vprop = p
noextract
let hmem (p:vprop) = hmem (hp_of p)
/// Abstract predicate for vprop implication. Currently implemented as an implication on the underlying slprop
val can_be_split (p q:pre_t) : Type0
/// Exposing the implementation of `can_be_split` when needed for proof purposes
val reveal_can_be_split (_:unit) : Lemma
(forall p q. can_be_split p q == Mem.slimp (hp_of p) (hp_of q))
/// A targeted version of the above
val can_be_split_interp (r r':vprop) (h:hmem r)
: Lemma (requires can_be_split r r')
(ensures interp (hp_of r') h)
/// A dependent version of can_be_split, to be applied to dependent postconditions
let can_be_split_forall (#a:Type) (p q:post_t a) = forall x. can_be_split (p x) (q x)
/// A version of can_be_split which is indexed by a proposition, which can be used for equalities abduction
let can_be_split_dep (p:prop) (t1 t2:pre_t) = p ==> can_be_split t1 t2
/// A dependent version of the above predicate
let can_be_split_forall_dep (#a:Type) (p:a -> prop) (t1 t2:post_t a) =
forall (x:a). p x ==> can_be_split (t1 x) (t2 x)
(* Some lemmas about the can_be_split* predicates,
to be used as rewriting rules for the abstract predicates *)
val can_be_split_trans (p q r:vprop)
: Lemma
(requires p `can_be_split` q /\ q `can_be_split` r)
(ensures p `can_be_split` r)
let can_be_split_trans_rev (p q r:vprop)
: Lemma
(requires q `can_be_split` r /\ p `can_be_split` q)
(ensures p `can_be_split` r)
= can_be_split_trans p q r
val can_be_split_star_l (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` p)
[SMTPat ((p `star` q) `can_be_split` p)]
val can_be_split_star_r (p q:vprop)
: Lemma
(ensures (p `star` q) `can_be_split` q)
[SMTPat ((p `star` q) `can_be_split` q)]
val can_be_split_refl (p:vprop)
: Lemma (p `can_be_split` p)
[SMTPat (p `can_be_split` p)]
val can_be_split_congr_l
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((p `star` r) `can_be_split` (q `star` r)))
val can_be_split_congr_r
(p q r: vprop)
: Lemma
(requires (p `can_be_split` q))
(ensures ((r `star` p) `can_be_split` (r `star` q)))
let prop_and (p1 p2: prop) : Tot prop = p1 /\ p2
let can_be_split_forall_dep_trans_rev
(#a: Type)
(cond1 cond2: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond2 q r /\ can_be_split_forall_dep cond1 p q))
(ensures (can_be_split_forall_dep (fun x -> cond1 x `prop_and` cond2 x) p r))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_trans x y) z)
let can_be_split_forall_dep_congr_l
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> p x `star` r x) (fun x -> q x `star` r x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_l x y) z)
let can_be_split_forall_dep_congr_r
(#a: Type)
(cond: a -> prop)
(p q r: post_t a)
: Lemma
(requires (can_be_split_forall_dep cond p q))
(ensures (can_be_split_forall_dep cond (fun x -> r x `star` p x) (fun x -> r x `star` q x)))
=
Classical.forall_intro_3 (fun x y z -> Classical.move_requires (can_be_split_congr_r x y) z)
/// To simplify the implementation of the framing tactic, dependent equivalence
/// is defined as a double dependent implication
let equiv_forall (#a:Type) (t1 t2:post_t a) : Type0
= t1 `can_be_split_forall` t2 /\ t2 `can_be_split_forall` t1
/// This equivalence models a context restriction at the end of a Steel computation;
/// note that t2 does not depend on the value of type `a`, but the two vprops must be
/// equivalent
let can_be_split_post (#a #b:Type) (t1:a -> post_t b) (t2:post_t b) =
forall (x:a). equiv_forall (t1 x) t2
/// Lifting the equivalence relation to vprops. Two vprops are equivalent if the underlying slprops
/// are equivalent
val equiv (p q:vprop) : prop
/// Revealing the definition of vprop equivalence when needed for proof purposes.
/// In other cases, the predicate is abstract
val reveal_equiv (p q:vprop) : Lemma (p `equiv` q <==> hp_of p `Mem.equiv` hp_of q)
(* A restricted view of the heap,
that only allows to access selectors of the current slprop *)
let rmem' (pre:vprop) =
FExt.restricted_g_t
(r0:vprop{can_be_split pre r0})
(fun r0 -> normal (t_of r0))
/// Ensuring that rmems encapsulate the structure induced by the separation logic star
val valid_rmem (#frame:vprop) (h:rmem' frame) : prop
unfold
let rmem (pre:vprop) = h:rmem' pre{valid_rmem h}
/// Exposing the definition of mk_rmem to better normalize Steel VCs
unfold noextract
let unrestricted_mk_rmem (r:vprop) (h:hmem r) = fun (r0:vprop{r `can_be_split` r0}) ->
can_be_split_interp r r0 h;
sel_of r0 h
[@@ __inner_steel_reduce__]
noextract
let mk_rmem' (r:vprop) (h:hmem r) : Tot (rmem' r) =
FExt.on_dom_g
(r0:vprop{r `can_be_split` r0})
(unrestricted_mk_rmem r h)
val lemma_valid_mk_rmem (r:vprop) (h:hmem r) : Lemma (valid_rmem (mk_rmem' r h))
[@@ __inner_steel_reduce__]
noextract
let mk_rmem (r:vprop) (h:hmem r) : Tot (rmem r) =
lemma_valid_mk_rmem r h;
mk_rmem' r h
val reveal_mk_rmem (r:vprop) (h:hmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (ensures reveal_can_be_split(); (mk_rmem r h) r0 == sel_of r0 h)
(* Logical pre and postconditions can only access the restricted view of the heap *)
type req_t (pre:pre_t) = rmem pre -> Type0
type ens_t (pre:pre_t) (a:Type) (post:post_t a) =
rmem pre -> (x:a) -> rmem (post x) -> Type0
(* Empty assertion *)
val emp : vprop
/// When needed for proof purposes, the empty assertion is a direct lift of the
/// empty assertion from Steel.Memory
val reveal_emp (_:unit) : Lemma (hp_of emp == Mem.emp /\ t_of emp == unit)
/// Lifting pure predicates to vprop
[@@__steel_reduce__]
unfold let pure (p:prop) = to_vprop (pure p)
/// Framing predicates for the Steel effect. If the current computation has already
/// been framed, then the additional frame is the empty predicate
let maybe_emp (framed:bool) (frame:pre_t) = if framed then frame == emp else True
/// Dependent version of the above predicate, usable in dependent postconditions
let maybe_emp_dep (#a:Type) (framed:bool) (frame:post_t a) =
if framed then (forall x. frame x == emp) else True
(* focus_rmem is an additional restriction of our view of memory.
We expose it here to be able to reduce through normalization;
Any valid application of focus_rmem h will be reduced to the application of h *)
[@@ __steel_reduce__]
unfold
let unrestricted_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
= fun (r':vprop{can_be_split r0 r'}) -> can_be_split_trans r r0 r'; h r'
[@@ __inner_steel_reduce__]
let focus_rmem' (#r: vprop) (h: rmem r) (r0: vprop{r `can_be_split` r0}) : Tot (rmem' r0)
= FExt.on_dom_g
(r':vprop{can_be_split r0 r'})
(unrestricted_focus_rmem h r0)
val lemma_valid_focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0})
: Lemma (valid_rmem (focus_rmem' h r0))
[@@ __inner_steel_reduce__]
let focus_rmem (#r:vprop) (h:rmem r) (r0:vprop{r `can_be_split` r0}) : Tot (rmem r0) =
lemma_valid_focus_rmem h r0;
focus_rmem' h r0
/// Exposing that calling focus_rmem on the current context corresponds to an equality
let focus_rmem_refl (r:vprop) (h:rmem r)
: Lemma (focus_rmem #r h r == h)
= FStar.FunctionalExtensionality.extensionality_g _ _ (focus_rmem #r h r) h
open FStar.Tactics.V2
/// State that all "atomic" subresources have the same selectors on both views.
/// The predicate has the __steel_reduce__ attribute, ensuring that VC normalization
/// will reduce it to a conjunction of equalities on atomic subresources
/// This predicate is also marked as `strict_on_arguments` on [frame], ensuring that
/// it will not be reduced when the frame is symbolic
/// Instead, the predicate will be rewritten to an equality using `lemma_frame_equalities` below
[@@ __steel_reduce__; strict_on_arguments [0]]
let rec frame_equalities'
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : Type0
= begin match frame with
| VUnit p -> h0 frame == h1 frame
| VStar p1 p2 ->
can_be_split_star_l p1 p2;
can_be_split_star_r p1 p2;
let h01 = focus_rmem h0 p1 in
let h11 = focus_rmem h1 p1 in
let h02 = focus_rmem h0 p2 in
let h12 = focus_rmem h1 p2 in
frame_equalities' p1 h01 h11 /\
frame_equalities' p2 h02 h12
end
/// This lemma states that frame_equalities is the same as an equality on the top-level frame.
/// The uncommon formulation with an extra [p] is needed to use in `rewrite_with_tactic`,
/// where the goal is of the shape `frame_equalities frame h0 h1 == ?u`
/// The rewriting happens below, in `frame_vc_norm`
val lemma_frame_equalities (frame:vprop) (h0:rmem frame) (h1:rmem frame) (p:Type0)
: Lemma
(requires (h0 frame == h1 frame) == p)
(ensures frame_equalities' frame h0 h1 == p)
/// A special case for frames about emp.
val lemma_frame_emp (h0:rmem emp) (h1:rmem emp) (p:Type0)
: Lemma (requires True == p)
(ensures frame_equalities' emp h0 h1 == p)
/// A variant of conjunction elimination, suitable to the equality goals during rewriting
val elim_conjunction (p1 p1' p2 p2':Type0)
: Lemma (requires p1 == p1' /\ p2 == p2')
(ensures (p1 /\ p2) == (p1' /\ p2'))
/// Normalization and rewriting step for generating frame equalities.
/// The frame_equalities function has the strict_on_arguments attribute on the [frame],
/// ensuring that it is not reduced when the frame is symbolic.
/// When that happens, we want to replace frame_equalities by an equality on the frame,
/// mimicking reduction
[@@plugin]
let frame_vc_norm () : Tac unit = with_compat_pre_core 0 (fun _ ->
// Do not normalize mk_rmem/focus_rmem to simplify application of
// the reflexivity lemma on frame_equalities'
norm [delta_attr [`%__steel_reduce__];
delta_only [`%Mkvprop'?.t; `%Mkvprop'?.hp; `%Mkvprop'?.sel;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__mult;
`%FStar.Algebra.CommMonoid.Equiv.__proj__CM__item__unit];
delta_qualifier ["unfold"];
iota;zeta;primops; simplify];
// After reduction, the term to rewrite might be of the shape
// (frame_equalities' ... /\ frame_equalities' .. /\ ...) == ?u,
// with some frame_equalities' possibly already fully reduced
// We repeatedly split the clause and extract the term on the left
// to generate equalities on atomic subresources
ignore (repeat (fun _ ->
// Try to split the conjunction. If there is no conjunction, we exit the repeat
apply_lemma (`elim_conjunction);
// Dismiss the two uvars created for the RHS, they'll be solved by unification
dismiss ();
dismiss ();
// The first goal is the left conjunction
split ();
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// Rewrites the frame_equalities if it wasn't yet reduced
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
// Finally solve the uvar, finishing the rewriting for this clause
trefl ()
));
// Removes the frame equality if it is about emp
or_else (fun _ -> apply_lemma (`lemma_frame_emp); dismiss()) (fun _ -> ());
// We do not have conjunctions anymore, we try to apply the frame_equalities rewriting
// If it fails, the frame was not symbolic, so there is nothing to do
or_else (fun _ -> apply_lemma (`lemma_frame_equalities); dismiss ()) (fun _ -> ());
norm normal_steps;
trefl ())
[@@ __steel_reduce__]
unfold
let frame_equalities
(frame:vprop)
(h0:rmem frame) (h1:rmem frame) : prop
= rewrite_with_tactic frame_vc_norm (frame_equalities' frame h0 h1)
/// More lemmas about the abstract can_be_split predicates, to be used as
/// rewriting rules in the tactic below
val can_be_split_dep_refl (p:vprop)
: Lemma (can_be_split_dep true_p p p)
val equiv_can_be_split (p1 p2:vprop) : Lemma
(requires p1 `equiv` p2)
(ensures p1 `can_be_split` p2)
val intro_can_be_split_frame (p q:vprop) (frame:vprop)
: Lemma (requires q `equiv` (p `star` frame))
(ensures can_be_split q p /\ True)
val can_be_split_post_elim (#a #b:Type) (t1:a -> post_t b) (t2:post_t b)
: Lemma (requires (forall (x:a) (y:b). t1 x y `equiv` t2 y))
(ensures t1 `can_be_split_post` t2)
val equiv_forall_refl (#a:Type) (t:post_t a)
: Lemma (t `equiv_forall` t)
val equiv_forall_elim (#a:Type) (t1 t2:post_t a)
: Lemma (requires (forall (x:a). t1 x `equiv` t2 x))
(ensures t1 `equiv_forall` t2)
open FStar.Tactics.CanonCommMonoidSimple.Equiv
(* equiv is an equivalence relation on vprops *)
/// Lemmas establishing the equivalence properties on equiv
val equiv_refl (x:vprop) : Lemma (equiv x x)
val equiv_sym (x y:vprop) : Lemma
(requires equiv x y)
(ensures equiv y x)
val equiv_trans (x y z:vprop) : Lemma
(requires equiv x y /\ equiv y z)
(ensures equiv x z)
module CE = FStar.Algebra.CommMonoid.Equiv
/// Equiv is an equivalence relation for vprops elements
inline_for_extraction noextract let req : CE.equiv vprop =
CE.EQ equiv
equiv_refl
equiv_sym
equiv_trans
(* Star induces a commutative monoid for the equiv equivalence relation *)
/// Lemmas establishing the commutative monoid properties
val cm_identity (x:vprop) : Lemma ((emp `star` x) `equiv` x)
val star_commutative (p1 p2:vprop)
: Lemma ((p1 `star` p2) `equiv` (p2 `star` p1))
val star_associative (p1 p2 p3:vprop)
: Lemma (((p1 `star` p2) `star` p3)
`equiv`
(p1 `star` (p2 `star` p3)))
val star_congruence (p1 p2 p3 p4:vprop)
: Lemma (requires p1 `equiv` p3 /\ p2 `equiv` p4)
(ensures (p1 `star` p2) `equiv` (p3 `star` p4))
/// Star induces a commutative monoid on vprops
[@__steel_reduce__]
inline_for_extraction noextract let rm : CE.cm vprop req =
CE.CM emp
star
cm_identity
star_associative
star_commutative
star_congruence
(*** Vprop combinators ***)
(* Refining a vprop with a selector predicate *)
/// Separation logic predicate stating the validity of a vprop with an additional refinement on its selector
val vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vrefine_hp (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: mem) : Lemma
(interp (vrefine_hp v p) m <==> (interp (hp_of v) m /\ p (sel_of v m)))
/// Selector type for a refined vprop
[@__steel_reduce__]
let vrefine_t (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot Type
= (x: t_of v {p x})
/// Selector of a refined vprop. Returns a value which satisfies the refinement predicate
val vrefine_sel (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot (selector (vrefine_t v p) (vrefine_hp v p))
/// Exposing the definition of the refined selector
val vrefine_sel_eq (v: vprop) (p: (normal (t_of v) -> Tot prop)) (m: Mem.hmem (vrefine_hp v p)) : Lemma
(
interp (hp_of v) m /\
vrefine_sel v p m == sel_of v m
)
// [SMTPat ((vrefine_sel v p) m)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above pieces to define a vprop refined by a selector prediacte
[@__steel_reduce__]
let vrefine' (v: vprop) (p: (normal (t_of v) -> Tot prop)) : Tot vprop' = {
hp = vrefine_hp v p;
t = vrefine_t v p;
sel = vrefine_sel v p;
}
[@__steel_reduce__]
let vrefine (v: vprop) (p: (normal (t_of v) -> Tot prop)) = VUnit (vrefine' v p)
(* Dependent star for vprops *)
/// Separation logic predicate corresponding to a dependent star,
/// where the second predicate depends on the selector value of the first
val vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (slprop u#1)
/// Exposing the validity of the above predicate when needed for proof purposes
val interp_vdep_hp (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: mem) : Lemma
(interp (vdep_hp v p) m <==> (interp (hp_of v) m /\ interp (hp_of v `Mem.star` hp_of (p (sel_of v m))) m))
/// Helper to define the selector type of the second component of the dependent star
let vdep_payload
(v: vprop) (p: ( (t_of v) -> Tot vprop))
(x: t_of v)
: Tot Type
= t_of (p x)
/// Selector type for the dependent star: A dependent tuple, where the second component's type depends on the first vprop
let vdep_t (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot Type
= dtuple2 (t_of v) (vdep_payload v p)
/// Selector for the dependent star
val vdep_sel (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot (selector (vdep_t v p) (vdep_hp v p))
/// Exposing the definition of the dependent star's selector when needed for proof purposes
val vdep_sel_eq (v: vprop) (p: ( (t_of v) -> Tot vprop)) (m: Mem.hmem (vdep_hp v p)) : Lemma
(
interp (hp_of v) m /\
begin let x = sel_of v m in
interp (hp_of (p x)) m /\
vdep_sel v p m == (| x, sel_of (p x) m |)
end
)
/// Combining the elements above to create a dependent star vprop
[@__steel_reduce__]
let vdep' (v: vprop) (p: ( (t_of v) -> Tot vprop)) : Tot vprop' = {
hp = vdep_hp v p;
t = vdep_t v p;
sel = vdep_sel v p;
}
[@__steel_reduce__]
let vdep (v: vprop) (p: ( (t_of v) -> Tot vprop)) = VUnit (vdep' v p)
(* Selector rewrite combinator *)
/// The selector of a rewrite combinator applies a function `f` to the current selector of a vprop.
val vrewrite_sel (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot (selector t (normal (hp_of v)))
/// Exposing the definition of the above selector
val vrewrite_sel_eq (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) (h: Mem.hmem (normal (hp_of v))) : Lemma
((vrewrite_sel v f <: selector' _ _) h == f ((normal (sel_of v) <: selector' _ _) h))
// [SMTPat (vrewrite_sel v f h)] // FIXME: this pattern causes Z3 "wrong number of argument" errors
/// Combining the above elements to create a rewrite vprop
[@__steel_reduce__]
let vrewrite' (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop' = {
hp = normal (hp_of v);
t = t;
sel = vrewrite_sel v f;
}
[@__steel_reduce__]
let vrewrite (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : Tot vprop = VUnit (vrewrite' v f)
(*** Framing tactic ***)
(* Specialize visit_tm from the standard F* tactic library to reimplement name_appears_in.
AF: As of Jan 14, 2021, calling name_appears_in from FStar.Tactics.Derived leads to a segfault *)
exception Appears
let on_sort_binder (f : term -> Tac unit) (b:binder) : Tac unit =
f b.sort
let rec visit_tm (ff : term -> Tac unit) (t : term) : Tac unit =
let tv = inspect t in
(match tv with
| Tv_FVar _
| Tv_UInst _ _
| Tv_Var _
| Tv_BVar _ -> ()
| Tv_Type _ -> ()
| Tv_Const c -> ()
| Tv_Uvar i u -> ()
| Tv_Unsupp -> ()
| Tv_Unknown -> ()
| Tv_Arrow b c ->
on_sort_binder ff b;
visit_comp ff c
| Tv_Abs b t ->
let b = on_sort_binder (visit_tm ff) b in
visit_tm ff t
| Tv_App l (r, q) ->
visit_tm ff l;
visit_tm ff r
| Tv_Refine b r ->
on_sort_binder ff b;
visit_tm ff r
| Tv_Let r attrs b def t ->
on_sort_binder ff b;
visit_tm ff def;
visit_tm ff t
| Tv_Match sc _ brs ->
visit_tm ff sc;
iter (visit_br ff) brs
| Tv_AscribedT e t topt _ ->
visit_tm ff e;
visit_tm ff t
| Tv_AscribedC e c topt _ ->
visit_tm ff e
); ff t
and visit_br (ff : term -> Tac unit) (b:branch) : Tac unit =
let (p, t) = b in
visit_tm ff t
and visit_comp (ff : term -> Tac unit) (c : comp) : Tac unit =
let cv = inspect_comp c in
match cv with
| C_Total ret -> visit_tm ff ret
| C_GTotal ret -> visit_tm ff ret
| C_Lemma pre post pats ->
visit_tm ff pre;
visit_tm ff post;
visit_tm ff pats
| C_Eff us eff res args decrs ->
visit_tm ff res;
iter (fun (a, q) -> visit_tm ff a) args;
iter (visit_tm ff) decrs
/// Decides whether a top-level name [nm] syntactically
/// appears in the term [t].
let name_appears_in (nm:name) (t:term) : Tac bool =
let ff (t : term) : Tac unit =
match inspect t with
| Tv_FVar fv -> if inspect_fv fv = nm then raise Appears
| t -> ()
in
try ignore (visit_tm ff t); false with
| Appears -> true
| e -> raise e
/// Checks whether term [t] appears in term [i]
let term_appears_in (t:term) (i:term) : Tac bool =
name_appears_in (explode_qn (term_to_string t)) i
/// We define a small language to handle arbitrary separation logic predicates.
/// Separation logic predicates are encoded as atoms for which equality is decidable,
/// here represented as integers
let atom : eqtype = int
let rec atoms_to_string (l:list atom) = match l with
| [] -> ""
| hd::tl -> string_of_int hd ^ " " ^ atoms_to_string tl
/// Reflecting the structure of our separation logic on atmos
type exp : Type =
| Unit : exp
| Mult : exp -> exp -> exp
| Atom : atom -> exp
/// A map from atoms to the terms they represent.
/// The second component of the term corresponds to a default element,
/// ensuring we never raise an exception when trying to access an element in the map
let amap (a:Type) = list (atom * a) * a
/// An empty atom map: The list map is empty
let const (#a:Type) (xa:a) : amap a = ([], xa)
/// Accessing an element in the atom map
// We reimplement List.Tot.Base.assoc because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_assoc (#key: eqtype) (#value: Type) (k: key) (dict: list (key & value)) : Pure (option value)
(requires True)
(ensures (fun res -> res == List.Tot.assoc k dict))
= match dict with
| [] -> None
| (k', v') :: q -> if k = k' then Some v' else my_assoc k q
let select (#a:Type) (x:atom) (am:amap a) : Tot a =
match my_assoc #atom #a x (fst am) with
| Some a -> a
| _ -> snd am
/// Updating the atom map. Since select finds the first element corresponding to
/// the atom in the list and we do not have any remove function,
/// we can simply append the new element at the head without removing any possible
/// previous element
let update (#a:Type) (x:atom) (xa:a) (am:amap a) : amap a =
(x, xa)::fst am, snd am
/// Check whether the current term is an unresolved vprop unification variable.
/// This can happen if either it is a uvar, or it is an unresolved dependent
/// vprop uvar which is applied to some argument
let is_uvar (t:term) : Tac bool = match inspect t with
| Tv_Uvar _ _ -> true
| Tv_App _ _ ->
let hd, args = collect_app t in
Tv_Uvar? (inspect hd)
| _ -> false
/// For a given term t, collect all terms in the list l with the same head symbol
let rec get_candidates (t:term) (l:list term) : Tac (list term) =
let name, _ = collect_app t in
match l with
| [] -> []
| hd::tl ->
let n, _ = collect_app hd in
if term_eq_old n name then (
hd::(get_candidates t tl)
) else get_candidates t tl
/// Try to remove a term that is exactly matching, not just that can be unified
let rec trivial_cancel (t:atom) (l:list atom) =
match l with
| [] -> false, l
| hd::tl ->
if hd = t then
// These elements match, we remove them
true, tl
else (let b, res = trivial_cancel t tl in b, hd::res)
/// Call trivial_cancel on all elements of l1.
/// The first two lists returned are the remainders of l1 and l2.
/// The last two lists are the removed parts of l1 and l2, with
/// the additional invariant that they are equal
let rec trivial_cancels (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, [], []
| hd::tl ->
let b, l2' = trivial_cancel hd l2 in
let l1', l2', l1_del, l2_del = trivial_cancels tl l2' am in
(if b then l1' else hd::l1'), l2',
(if b then hd::l1_del else l1_del), (if b then hd::l2_del else l2_del)
exception Failed
exception Success
/// Helper to print the terms corresponding to the current list of atoms
let rec print_atoms (l:list atom) (am:amap term) : Tac string =
match l with
| [] -> ""
| [hd] -> term_to_string (select hd am)
| hd::tl -> term_to_string (select hd am) ^ " * " ^ print_atoms tl am
/// For a list of candidates l, count the number that can unify with t.
/// Does not try to unify with a uvar, this will be done at the very end.
/// Tries to unify with slprops with a different head symbol, it might
/// be an abbreviation
let rec try_candidates (t:atom) (l:list atom) (am:amap term) : Tac (atom * int) =
match l with
| [] -> t, 0
| hd::tl ->
if is_uvar (select hd am) then (try_candidates t tl am)
else
// Encapsulate unify in a try/with to ensure unification is not actually performed
let res = try if unify (select t am) (select hd am) then raise Success else raise Failed
with | Success -> true | _ -> false in
let t', n' = try_candidates t tl am in
if res && hd <> t' then hd, 1 + n' else t', n'
/// Remove the given term from the list. Only to be called when
/// try_candidates succeeded
let rec remove_from_list (t:atom) (l:list atom) : Tac (list atom) =
match l with
| [] -> fail "atom in remove_from_list not found: should not happen"; []
| hd::tl -> if t = hd then tl else hd::remove_from_list t tl
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
let rec equivalent_lists_once (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l1 with
| [] -> [], l2, l1_del, l2_del
| hd::tl ->
let t, n = try_candidates hd l2 am in
if n = 1 then (
let l2 = remove_from_list t l2 in
equivalent_lists_once tl l2 (hd::l1_del) (t::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once tl l2 l1_del l2_del am in
hd::rem1, rem2, l1'_del, l2'_del
)
/// Check if two lists of slprops are equivalent by recursively calling
/// try_candidates by iterating on l2.
/// Assumes that only l2 contains terms with the head symbol unresolved.
/// It returns all elements that were not resolved during this iteration *)
/// This is very close to equivalent_lists_once above, but helps making progress
/// when l1 contains syntactically equal candidates
let rec equivalent_lists_once_l2 (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * list atom * list atom) =
match l2 with
| [] -> l1, [], l1_del, l2_del
| hd::tl ->
if is_uvar (select hd am) then
// We do not try to match the vprop uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
else (
let t, n = try_candidates hd l1 am in
if n = 1 then (
let l1 = remove_from_list t l1 in
equivalent_lists_once_l2 l1 tl (t::l1_del) (hd::l2_del) am
) else (
// Either too many candidates for this scrutinee, or no candidate but the uvar
let rem1, rem2, l1'_del, l2'_del = equivalent_lists_once_l2 l1 tl l1_del l2_del am in
rem1, hd::rem2, l1'_del, l2'_del
)
)
let get_head (l:list atom) (am:amap term) : term = match l with
| [] -> `()
| hd::_ -> select hd am
/// Checks whether the list of atoms [l] only contains one unresolved uvar
let is_only_uvar (l:list atom) (am:amap term) : Tac bool =
if List.Tot.Base.length l = 1 then is_uvar (select (List.Tot.Base.hd l) am)
else false
/// Assumes that u is a uvar, checks that all variables in l can be unified with it.
/// Later in the tactic, the uvar will be unified to a star of l *)
let rec try_unifying_remaining (l:list atom) (u:term) (am:amap term) : Tac unit =
match l with
| [] -> ()
| hd::tl ->
try if unify u (select hd am) then raise Success else raise Failed with
| Success -> try_unifying_remaining tl u am
| _ -> fail ("could not find candidate for scrutinee " ^ term_to_string (select hd am))
/// Is SMT rewriting enabled for this binder
let is_smt_binder (b:binder) : Tac bool =
let l = b.attrs in
not (List.Tot.isEmpty (filter (fun t -> is_fvar t (`%smt_fallback)) l))
/// Creates a new term, where all arguments where SMT rewriting is enabled have been replaced
/// by fresh, unconstrained unification variables
let rec new_args_for_smt_attrs (env:env) (l:list argv) (ty:typ) : Tac (list argv * list term) =
let fresh_ghost_uvar ty =
let e = cur_env () in
ghost_uvar_env e ty
in
match l, inspect_unascribe ty with
| (arg, aqualv)::tl, Tv_Arrow binder comp ->
let needs_smt = is_smt_binder binder in
let new_hd =
if needs_smt then (
let arg_ty = tc env arg in
let uvar = fresh_ghost_uvar arg_ty in
unshelve uvar;
flip ();
(uvar, aqualv)
) else (arg, aqualv)
in
begin
let ty2 =
match inspect_comp comp with
| C_Total ty2 -> ty2
| C_Eff _ eff_name ty2 _ _ ->
if eff_name = ["Prims"; "Tot"]
then ty2
else fail "computation type not supported in definition of slprops"
| _ -> fail "computation type not supported in definition of slprops" in
let tl_argv, tl_terms = new_args_for_smt_attrs env tl ty2 in
new_hd::tl_argv, (if needs_smt then arg::tl_terms else tl_terms)
end
| [], Tv_FVar fv -> [], []
| _ -> fail "should not happen. Is an slprop partially applied?"
/// Rewrites all terms in the context to enable SMT rewriting through the use of fresh, unconstrained unification variables
let rewrite_term_for_smt (env:env) (am:amap term * list term) (a:atom) : Tac (amap term * list term)
= let am, prev_uvar_terms = am in
let term = select a am in
let hd, args = collect_app term in
let t = tc env hd in
let new_args, uvar_terms = new_args_for_smt_attrs env args t in
let new_term = mk_app hd new_args in
update a new_term am, List.Tot.append uvar_terms prev_uvar_terms
/// User-facing error message when the framing tactic fails
let fail_atoms (#a:Type) (l1 l2:list atom) (am:amap term) : Tac a
= fail ("could not find a solution for unifying\n" ^ print_atoms l1 am ^ "\nand\n" ^ print_atoms l2 am)
/// Variant of equivalent_lists' below to be called once terms have been rewritten to allow SMT rewriting.
/// If unification succeeds and we have unicity of the solution, this tactic will succeed,
/// and ultimately create an SMT guard that the two terms are actually equal
let rec equivalent_lists_fallback (n:nat) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false)
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then
// Should always be smaller or equal to n
// If it is equal, no progress was made.
fail_atoms rem1 rem2 am
else equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' am
/// Iterates over all terms in [l2] to prepare them for unification with SMT rewriting
let replace_smt_uvars (l1 l2:list atom) (am:amap term) : Tac (amap term * list term)
= let env = cur_env () in
fold_left (rewrite_term_for_smt env) (am, []) l2
/// Recursively calls equivalent_lists_once.
/// Stops when we're done with unification, or when we didn't make any progress
/// If we didn't make any progress, we have too many candidates for some terms.
/// Accumulates rewritings of l1 and l2 in l1_del and l2_del, with the invariant
/// that the two lists are unifiable at any point
/// The boolean indicates if there is a leftover empty frame
let rec equivalent_lists' (n:nat) (use_smt:bool) (l1 l2 l1_del l2_del:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term) =
match l1 with
| [] -> begin match l2 with
| [] -> (l1_del, l2_del, false, [])
| [hd] ->
// Succeed if there is only one uvar left in l2, which can be therefore
// be unified with emp
if is_uvar (select hd am) then (
// xsdenote is left associative: We put hd at the top to get
// ?u `star` p <==> emp `star` p
(l1_del, hd :: l2_del, true, []))
else fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
| _ -> fail ("could not find candidates for " ^ term_to_string (get_head l2 am))
end
| _ ->
if is_only_uvar l2 am then (
// Terms left in l1, but only a uvar left in l2.
// Put all terms left at the end of l1_rem, so that they can be unified
// with exactly the uvar because of the structure of xsdenote
try_unifying_remaining l1 (get_head l2 am) am;
l1_del `List.Tot.append` l1, l2_del `List.Tot.append` l2, false, []
) else
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once l1 l2 l1_del l2_del am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Try to make progress by matching non-uvars of l2 with candidates in l1
let rem1, rem2, l1_del', l2_del' = equivalent_lists_once_l2 rem1 rem2 l1_del' l2_del' am in
let n' = List.Tot.length rem1 in
if n' >= n then (
// Should always be smaller or equal to n
// If it is equal, no progress was made.
if use_smt then
// SMT fallback is allowed
let new_am, uvar_terms = replace_smt_uvars rem1 rem2 am in
let l1_f, l2_f, b = equivalent_lists_fallback n' rem1 rem2 l1_del' l2_del' new_am in
l1_f, l2_f, b, uvar_terms
else fail_atoms rem1 rem2 am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
) else equivalent_lists' n' use_smt rem1 rem2 l1_del' l2_del' am
/// Checks if term for atom t unifies with fall uvars in l
let rec unifies_with_all_uvars (t:term) (l:list atom) (am:amap term) : Tac bool =
match l with
| [] -> true
| hd::tl ->
if unifies_with_all_uvars t tl am then (
// Unified with tail, try this term
let hd_t = select hd am in
if is_uvar hd_t then (
// The head term is a uvar, try unifying
try if unify t hd_t then raise Success else raise Failed
with | Success -> true | _ -> false
) else true // The uvar is not a head term, we do not need to try it
) else false
/// Puts all terms in l1 that cannot unify with the uvars in l2 at the top:
/// They need to be solved first
let rec most_restricted_at_top (l1 l2:list atom) (am:amap term) : Tac (list atom) =
match l1 with
| [] -> []
| hd::tl ->
if unifies_with_all_uvars (select hd am) l2 am then (most_restricted_at_top tl l2 am) `List.Tot.append` [hd]
else hd::(most_restricted_at_top tl l2 am)
/// Core AC-unification tactic.
/// First remove all trivially equal terms, then try to decide equivalence.
/// Assumes that l1 does not contain any vprop uvar.
/// If it succeeds, returns permutations of l1, l2, and a boolean indicating
/// if l2 has a trailing empty frame to be unified
let equivalent_lists (use_smt:bool) (l1 l2:list atom) (am:amap term)
: Tac (list atom * list atom * bool * list term)
= let l1, l2, l1_del, l2_del = trivial_cancels l1 l2 am in
let l1 = most_restricted_at_top l1 l2 am in
let n = List.Tot.length l1 in
let l1_del, l2_del, emp_frame, uvar_terms = equivalent_lists' n use_smt l1 l2 l1_del l2_del am in
l1_del, l2_del, emp_frame, uvar_terms
(* Helpers to relate the actual terms to their representation as a list of atoms *)
open FStar.Reflection.V2.Derived.Lemmas
let rec list_to_string (l:list term) : Tac string =
match l with
| [] -> "end"
| hd::tl -> term_to_string hd ^ " " ^ list_to_string tl
let rec mdenote_gen (#a:Type u#aa) (unit:a) (mult:a -> a -> a) (am:amap a) (e:exp) : a =
match e with
| Unit -> unit
| Atom x -> select x am
| Mult e1 e2 -> mult (mdenote_gen unit mult am e1) (mdenote_gen unit mult am e2)
let rec xsdenote_gen (#a:Type) (unit:a) (mult:a -> a -> a) (am:amap a) (xs:list atom) : a =
match xs with
| [] -> unit
| [x] -> select x am
| x::xs' -> mult (select x am) (xsdenote_gen unit mult am xs')
unfold
let mdenote (#a:Type u#aa) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp) : a =
let open FStar.Algebra.CommMonoid.Equiv in
mdenote_gen (CM?.unit m) (CM?.mult m) am e
unfold
let xsdenote (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom) : a =
let open FStar.Algebra.CommMonoid.Equiv in
xsdenote_gen (CM?.unit m) (CM?.mult m) am xs
// We reimplement List.Tot.Base.append because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_append (#t: Type) (l1 l2: list t) : Pure (list t)
(requires True)
(ensures (fun res -> res == l1 `List.Tot.append` l2))
(decreases l1)
= match l1 with
| [] -> l2
| a :: q -> a :: my_append q l2
let rec flatten (e:exp) : list atom =
match e with
| Unit -> []
| Atom x -> [x]
| Mult e1 e2 -> flatten e1 `my_append` flatten e2
let rec flatten_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs1 xs2:list atom)
: Lemma (xsdenote eq m am (xs1 `my_append` xs2) `CE.EQ?.eq eq` CE.CM?.mult m (xsdenote eq m am xs1)
(xsdenote eq m am xs2)) =
let open FStar.Algebra.CommMonoid.Equiv in
match xs1 with
| [] ->
CM?.identity m (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.unit m) (xsdenote eq m am xs2)) (xsdenote eq m am xs2)
| [x] -> (
if (Nil? xs2)
then (right_identity eq m (select x am);
EQ?.symmetry eq (CM?.mult m (select x am) (CM?.unit m)) (select x am))
else EQ?.reflexivity eq (CM?.mult m (xsdenote eq m am [x]) (xsdenote eq m am xs2)))
| x::xs1' ->
flatten_correct_aux eq m am xs1' xs2;
EQ?.reflexivity eq (select x am);
CM?.congruence m (select x am) (xsdenote eq m am (xs1' `my_append` xs2))
(select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2));
CM?.associativity m (select x am) (xsdenote eq m am xs1') (xsdenote eq m am xs2);
EQ?.symmetry eq (CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)));
EQ?.transitivity eq (CM?.mult m (select x am) (xsdenote eq m am (xs1' `my_append` xs2)))
(CM?.mult m (select x am) (CM?.mult m (xsdenote eq m am xs1') (xsdenote eq m am xs2)))
(CM?.mult m (CM?.mult m (select x am) (xsdenote eq m am xs1')) (xsdenote eq m am xs2))
let rec flatten_correct (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e:exp)
: Lemma (mdenote eq m am e `CE.EQ?.eq eq` xsdenote eq m am (flatten e)) =
let open FStar.Algebra.CommMonoid.Equiv in
match e with
| Unit -> EQ?.reflexivity eq (CM?.unit m)
| Atom x -> EQ?.reflexivity eq (select x am)
| Mult e1 e2 ->
flatten_correct_aux eq m am (flatten e1) (flatten e2);
EQ?.symmetry eq (xsdenote eq m am (flatten e1 `my_append` flatten e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)));
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CM?.congruence m (mdenote eq m am e1) (mdenote eq m am e2)
(xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2));
EQ?.transitivity eq (CM?.mult m (mdenote eq m am e1) (mdenote eq m am e2))
(CM?.mult m (xsdenote eq m am (flatten e1)) (xsdenote eq m am (flatten e2)))
(xsdenote eq m am (flatten e1 `my_append` flatten e2))
let monoid_reflect (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (e1 e2:exp)
(_ : squash (xsdenote eq m am (flatten e1) `CE.EQ?.eq eq` xsdenote eq m am (flatten e2)))
: squash (mdenote eq m am e1 `CE.EQ?.eq eq` mdenote eq m am e2) =
flatten_correct eq m am e1;
flatten_correct eq m am e2;
CE.EQ?.symmetry eq (mdenote eq m am e2) (xsdenote eq m am (flatten e2));
CE.EQ?.transitivity eq
(xsdenote eq m am (flatten e1))
(xsdenote eq m am (flatten e2))
(mdenote eq m am e2);
CE.EQ?.transitivity eq
(mdenote eq m am e1)
(xsdenote eq m am (flatten e1))
(mdenote eq m am e2)
// Here we sort the variable numbers
// We reimplement List.Tot.Base.sortWith because we need our tactic to normalize it,
// but we don't want to normalize user-provided instances
let rec my_partition (#a: Type) (f: (a -> Tot bool)) (l: list a)
: Pure (list a & list a)
(requires True)
(ensures (fun res -> res == List.Tot.partition f l))
= match l with
| [] -> [], []
| hd::tl ->
let l1, l2 = my_partition f tl in
if f hd
then hd::l1, l2
else l1, hd::l2
let rec partition_ext (#a: Type) (f1 f2: (a -> Tot bool)) (l: list a)
: Lemma
(requires (forall x . f1 x == f2 x))
(ensures (List.Tot.partition f1 l == List.Tot.partition f2 l))
= match l with
| [] -> ()
| hd::tl -> partition_ext f1 f2 tl
let my_bool_of_compare (#a: Type) (f: a -> a -> Tot int) (x: a) (y: a) : Tot bool
= f x y < 0
let rec my_sortWith (#a: Type) (f: (a -> a -> Tot int)) (l:list a)
: Pure (list a)
(requires True)
(ensures (fun res -> res == List.Tot.sortWith f l))
(decreases (List.Tot.length l))
= match l with
| [] -> []
| pivot::tl ->
let hi, lo = my_partition (my_bool_of_compare f pivot) tl in
partition_ext (my_bool_of_compare f pivot) (List.Tot.bool_of_compare f pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f pivot) tl;
my_append (my_sortWith f lo) (pivot::my_sortWith f hi)
let rec sortWith_ext (#a: Type) (f1 f2: (a -> a -> Tot int)) (l: list a)
: Lemma
(requires (forall x y . f1 x y == f2 x y))
(ensures (List.Tot.sortWith f1 l == List.Tot.sortWith f2 l))
(decreases (List.Tot.length l))
= match l with
| [] -> ()
| pivot::tl ->
partition_ext (List.Tot.bool_of_compare f1 pivot) (List.Tot.bool_of_compare f2 pivot) tl;
List.Tot.partition_length (List.Tot.bool_of_compare f1 pivot) tl;
let hi, lo = List.Tot.partition (List.Tot.bool_of_compare f1 pivot) tl in
sortWith_ext f1 f2 lo;
sortWith_ext f1 f2 hi
let permute = list atom -> list atom
let my_compare_of_bool (#a:eqtype) (rel: a -> a -> Tot bool) (x: a) (y: a) : Tot int
= if x `rel` y then -1
else if x = y then 0
else 1
let sort : permute = my_sortWith #int (my_compare_of_bool (<))
#push-options "--fuel 1 --ifuel 1"
let lemma_xsdenote_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (hd:atom) (tl:list atom)
: Lemma (xsdenote eq m am (hd::tl) `CE.EQ?.eq eq`
(CE.CM?.mult m (select hd am) (xsdenote eq m am tl)))
= let open FStar.Algebra.CommMonoid.Equiv in
match tl with
| [] ->
assert (xsdenote eq m am (hd::tl) == select hd am);
CM?.identity m (select hd am);
EQ?.symmetry eq (CM?.unit m `CM?.mult m` select hd am) (select hd am);
CM?.commutativity m (CM?.unit m) (select hd am);
EQ?.transitivity eq
(xsdenote eq m am (hd::tl))
(CM?.unit m `CM?.mult m` select hd am)
(CM?.mult m (select hd am) (xsdenote eq m am tl))
| _ -> EQ?.reflexivity eq (xsdenote eq m am (hd::tl))
let rec partition_equiv (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (pivot:atom) (q:list atom)
: Lemma
(let open FStar.List.Tot.Base in
let hi, lo = partition (bool_of_compare (compare_of_bool (<)) pivot) q in
CE.EQ?.eq eq
(xsdenote eq m am hi `CE.CM?.mult m` xsdenote eq m am lo)
(xsdenote eq m am q))
= let open FStar.Algebra.CommMonoid.Equiv in
let open FStar.List.Tot.Base in
let f = bool_of_compare (compare_of_bool (<)) pivot in
let hi, lo = partition f q in
match q with
| [] -> CM?.identity m (xsdenote eq m am hi)
| hd::tl ->
let l1, l2 = partition f tl in
partition_equiv eq m am pivot tl;
assert (EQ?.eq eq
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(xsdenote eq m am tl));
EQ?.reflexivity eq (xsdenote eq m am l1);
EQ?.reflexivity eq (xsdenote eq m am l2);
EQ?.reflexivity eq (xsdenote eq m am hi);
EQ?.reflexivity eq (xsdenote eq m am lo);
if f hd then begin
assert (hi == hd::l1 /\ lo == l2);
lemma_xsdenote_aux eq m am hd l1;
CM?.congruence m
(xsdenote eq m am hi)
(xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am l1)
(xsdenote eq m am l2);
CM?.associativity m
(select hd am)
(xsdenote eq m am l1)
(xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
((select hd am `CM?.mult m` xsdenote eq m am l1) `CM?.mult m` xsdenote eq m am l2)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2));
EQ?.reflexivity eq (select hd am);
CM?.congruence m
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(select hd am)
(xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2))
(select hd am `CM?.mult m` xsdenote eq m am tl);
lemma_xsdenote_aux eq m am hd tl;
EQ?.symmetry eq
(xsdenote eq m am (hd::tl))
(select hd am `CM?.mult m` xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am tl)
(xsdenote eq m am (hd::tl))
end else begin
assert (hi == l1 /\ lo == hd::l2);
lemma_xsdenote_aux eq m am hd l2;
CM?.congruence m
(xsdenote eq m am hi)
(xsdenote eq m am lo)
(xsdenote eq m am l1)
(select hd am `CM?.mult m` xsdenote eq m am l2);
CM?.commutativity m
(xsdenote eq m am l1)
(select hd am `CM?.mult m` xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(xsdenote eq m am l1 `CM?.mult m` (select hd am `CM?.mult m` xsdenote eq m am l2))
((select hd am `CM?.mult m` xsdenote eq m am l2) `CM?.mult m` xsdenote eq m am l1);
CM?.associativity m
(select hd am)
(xsdenote eq m am l2)
(xsdenote eq m am l1);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
((select hd am `CM?.mult m` xsdenote eq m am l2) `CM?.mult m` xsdenote eq m am l1)
(select hd am `CM?.mult m` (xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1));
CM?.commutativity m (xsdenote eq m am l2) (xsdenote eq m am l1);
EQ?.reflexivity eq (select hd am);
CM?.congruence m
(select hd am)
(xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1)
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l2 `CM?.mult m` xsdenote eq m am l1))
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2));
CM?.congruence m
(select hd am)
(xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2)
(select hd am)
(xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` (xsdenote eq m am l1 `CM?.mult m` xsdenote eq m am l2))
(select hd am `CM?.mult m` xsdenote eq m am tl);
lemma_xsdenote_aux eq m am hd tl;
EQ?.symmetry eq
(xsdenote eq m am (hd::tl))
(select hd am `CM?.mult m` xsdenote eq m am tl);
EQ?.transitivity eq
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select hd am `CM?.mult m` xsdenote eq m am tl)
(xsdenote eq m am (hd::tl))
end
let rec sort_correct_aux (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (xs:list atom)
: Lemma (requires True)
(ensures xsdenote eq m am xs `CE.EQ?.eq eq` xsdenote eq m am (sort xs))
(decreases (FStar.List.Tot.Base.length xs))
= let open FStar.Algebra.CommMonoid.Equiv in
match xs with
| [] -> EQ?.reflexivity eq (xsdenote eq m am [])
| pivot::q ->
let sort0 : permute = List.Tot.sortWith #int (List.Tot.compare_of_bool (<)) in
let sort_eq (l: list atom) : Lemma
(sort l == sort0 l)
[SMTPat (sort l)]
= sortWith_ext (my_compare_of_bool (<)) (List.Tot.compare_of_bool (<)) l
in
let open FStar.List.Tot.Base in
let f:int -> int -> int = compare_of_bool (<) in
let hi, lo = partition (bool_of_compare f pivot) q in
flatten_correct_aux eq m am (sort lo) (pivot::sort hi);
assert (xsdenote eq m am (sort xs) `EQ?.eq eq`
CM?.mult m (xsdenote eq m am (sort lo))
(xsdenote eq m am (pivot::sort hi)));
lemma_xsdenote_aux eq m am pivot (sort hi);
EQ?.reflexivity eq (xsdenote eq m am (sort lo));
CM?.congruence m
(xsdenote eq m am (sort lo))
(xsdenote eq m am (pivot::sort hi))
(xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` xsdenote eq m am (sort hi));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` xsdenote eq m am (pivot::sort hi))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi))));
CM?.commutativity m
(xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` xsdenote eq m am (sort hi));
CM?.associativity m
(select pivot am)
(xsdenote eq m am (sort hi))
(xsdenote eq m am (sort lo));
EQ?.transitivity eq
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)))
((select pivot am `CM?.mult m` xsdenote eq m am (sort hi)) `CM?.mult m` xsdenote eq m am (sort lo))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(xsdenote eq m am (sort lo) `CM?.mult m` (select pivot am `CM?.mult m` xsdenote eq m am (sort hi)))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))));
partition_length (bool_of_compare f pivot) q;
sort_correct_aux eq m am hi;
sort_correct_aux eq m am lo;
EQ?.symmetry eq (xsdenote eq m am lo) (xsdenote eq m am (sort lo));
EQ?.symmetry eq (xsdenote eq m am hi) (xsdenote eq m am (sort hi));
CM?.congruence m
(xsdenote eq m am (sort hi))
(xsdenote eq m am (sort lo))
(xsdenote eq m am hi)
(xsdenote eq m am lo);
assert (EQ?.eq eq
(xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo));
EQ?.reflexivity eq (select pivot am);
CM?.congruence m
(select pivot am)
(xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo))
(select pivot am)
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo);
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am (sort hi) `CM?.mult m` xsdenote eq m am (sort lo)))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)));
partition_equiv eq m am pivot q;
CM?.congruence m
(select pivot am)
(xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo)
(select pivot am)
(xsdenote eq m am q);
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am hi `CM?.mult m` xsdenote eq m am lo))
(select pivot am `CM?.mult m` (xsdenote eq m am q));
assert (EQ?.eq eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am q)));
lemma_xsdenote_aux eq m am pivot q;
EQ?.symmetry eq
(xsdenote eq m am (pivot::q))
(select pivot am `CM?.mult m` (xsdenote eq m am q));
EQ?.transitivity eq
(xsdenote eq m am (sort xs))
(select pivot am `CM?.mult m` (xsdenote eq m am q))
(xsdenote eq m am xs);
EQ?.symmetry eq (xsdenote eq m am (sort xs)) (xsdenote eq m am xs)
#pop-options
#push-options "--fuel 0 --ifuel 0"
(* Lemmas to be called after a permutation compatible with AC-unification was found *)
let smt_reflexivity (#a:Type) (eq:CE.equiv a) (x y:a)
: Lemma (requires x == y)
(ensures CE.EQ?.eq eq x y)
= CE.EQ?.reflexivity eq x
let identity_left_smt (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x y:a)
: Lemma
(requires x == y)
(ensures CE.EQ?.eq eq x (CE.CM?.mult m (CE.CM?.unit m) y))
= CE.CM?.identity m x;
CE.EQ?.symmetry eq (CE.CM?.mult m (CE.CM?.unit m) x) x
let identity_left (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x:a)
: Lemma (CE.EQ?.eq eq x (CE.CM?.mult m (CE.CM?.unit m) x))
= CE.CM?.identity m x;
CE.EQ?.symmetry eq (CE.CM?.mult m (CE.CM?.unit m) x) x
let identity_right_diff (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (x y:a) : Lemma
(requires CE.EQ?.eq eq x y)
(ensures CE.EQ?.eq eq (CE.CM?.mult m x (CE.CM?.unit m)) y)
= CE.right_identity eq m x;
CE.EQ?.transitivity eq (CE.CM?.mult m x (CE.CM?.unit m)) x y
/// Dismiss possible vprops goals that might have been created by lemma application.
/// These vprops will be instantiated at a later stage; else, Meta-F* will raise an error
let rec dismiss_slprops () : Tac unit =
match term_as_formula' (cur_goal ()) with
| App t _ -> if is_fvar t (`%squash) then () else (dismiss(); dismiss_slprops ())
| _ -> dismiss(); dismiss_slprops ()
/// Recursively removing trailing empty assertions
let rec n_identity_left (n:int) (eq m:term) : Tac unit
= if n = 0 then (
apply_lemma (`(CE.EQ?.reflexivity (`#eq)));
// Cleaning up, in case a uvar has been generated here. It'll be solved later
set_goals [])
else (
apply_lemma (`identity_right_diff (`#eq) (`#m));
// Drop the slprops generated, they will be solved later
dismiss_slprops ();
n_identity_left (n-1) eq m
)
/// Helper lemma: If two vprops (as represented by lists of atoms) are equivalent, then their canonical forms
/// (corresponding to applying the sort function on atoms) are equivalent
let equivalent_sorted (#a:Type) (eq:CE.equiv a) (m:CE.cm a eq) (am:amap a) (l1 l2 l1' l2':list atom)
: Lemma (requires
sort l1 == sort l1' /\
sort l2 == sort l2' /\
xsdenote eq m am l1 `CE.EQ?.eq eq` xsdenote eq m am l2)
(ensures xsdenote eq m am l1' `CE.EQ?.eq eq` xsdenote eq m am l2')
= let open FStar.Algebra.CommMonoid.Equiv in
sort_correct_aux eq m am l1';
sort_correct_aux eq m am l1;
EQ?.symmetry eq (xsdenote eq m am l1) (xsdenote eq m am (sort l1));
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am (sort l1'))
(xsdenote eq m am l1);
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am l1)
(xsdenote eq m am l2);
sort_correct_aux eq m am l2;
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am l2)
(xsdenote eq m am (sort l2));
sort_correct_aux eq m am l2';
EQ?.symmetry eq (xsdenote eq m am l2') (xsdenote eq m am (sort l2'));
EQ?.transitivity eq
(xsdenote eq m am l1')
(xsdenote eq m am (sort l2))
(xsdenote eq m am l2')
#pop-options
/// Finds the position of first occurrence of x in xs.
/// This is now specialized to terms and their funny term_eq_old.
let rec where_aux (n:nat) (x:term) (xs:list term) :
Tac (option nat) (decreases xs) =
match xs with
| [] -> None
| x'::xs' -> if term_eq_old x x' then Some n else where_aux (n+1) x xs'
let where = where_aux 0
let fatom (t:term) (ts:list term) (am:amap term) : Tac (exp * list term * amap term) =
match where t ts with
| Some v -> (Atom v, ts, am)
| None ->
let vfresh = List.Tot.Base.length ts in
let t = norm_term [iota; zeta] t in
(Atom vfresh, ts `List.Tot.append` [t], update vfresh t am)
/// Transforming a term into the corresponding list of atoms
/// If the atomic terms were already present in the map [am], then
/// they correspond to the same atoms
/// This expects that mult, unit, and t have already been normalized
let rec reification_aux (ts:list term) (am:amap term)
(mult unit t : term) : Tac (exp * list term * amap term) =
let hd, tl = collect_app_ref t in
match inspect_unascribe hd, List.Tot.Base.list_unref tl with
| Tv_FVar fv, [(t1, Q_Explicit) ; (t2, Q_Explicit)] ->
if term_eq_old (pack (Tv_FVar fv)) mult
then (let (e1, ts, am) = reification_aux ts am mult unit t1 in
let (e2, ts, am) = reification_aux ts am mult unit t2 in
(Mult e1 e2, ts, am))
else fatom t ts am
| _, _ ->
if term_eq_old t unit
then (Unit, ts, am)
else fatom t ts am
/// Performs the required normalization before calling the function above
let reification (eq: term) (m: term) (ts:list term) (am:amap term) (t:term) :
Tac (exp * list term * amap term) =
let mult = norm_term [iota; zeta; delta] (`CE.CM?.mult (`#m)) in
let unit = norm_term [iota; zeta; delta] (`CE.CM?.unit (`#m)) in
let t = norm_term [iota; zeta] t in
reification_aux ts am mult unit t
/// Meta-F* internal: Transforms the atom map into a term
let rec convert_map (m : list (atom * term)) : term =
match m with
| [] -> `[]
| (a, t)::ps ->
let a = pack (Tv_Const (C_Int a)) in
(* let t = norm_term [delta] t in *)
`((`#a, (`#t)) :: (`#(convert_map ps)))
/// `am` is an amap (basically a list) of terms, each representing a value
/// of type `a` (whichever we are canonicalizing). This functions converts
/// `am` into a single `term` of type `amap a`, suitable to call `mdenote` with *)
let convert_am (am : amap term) : term =
let (map, def) = am in
(* let def = norm_term [delta] def in *)
`( (`#(convert_map map), `#def) )
/// Transforms a term representatoin into a term through quotation
let rec quote_exp (e:exp) : term =
match e with
| Unit -> (`Unit)
| Mult e1 e2 -> (`Mult (`#(quote_exp e1)) (`#(quote_exp e2)))
| Atom n -> let nt = pack (Tv_Const (C_Int n)) in
(`Atom (`#nt))
let rec quote_atoms (l:list atom) = match l with
| [] -> `[]
| hd::tl -> let nt = pack (Tv_Const (C_Int hd)) in
(`Cons (`#nt) (`#(quote_atoms tl)))
/// Some internal normalization steps to make reflection of vprops into atoms and atom permutation go smoothly.
/// We reimplemented sorting/list functions to normalize our uses without normalizing those introduced by the user.
let normal_tac_steps = [primops; iota; zeta; delta_only [
`%mdenote; `%select;
`%my_assoc; `%my_append;
`%flatten; `%sort;
`%my_sortWith; `%my_partition;
`%my_bool_of_compare; `%my_compare_of_bool;
`%fst; `%__proj__Mktuple2__item___1;
`%snd; `%__proj__Mktuple2__item___2;
`%CE.__proj__CM__item__unit;
`%CE.__proj__CM__item__mult;
`%rm]]
/// The normalization function, using the above normalization steps
let normal_tac (#a:Type) (x:a) : a = FStar.Pervasives.norm normal_tac_steps x
/// Helper lemma to establish relation between normalized and initial values
let normal_elim (x:Type0) : Lemma
(requires x)
(ensures normal_tac x)
= ()
exception Result of list atom * list atom * bool * list term
/// F* equalities are typed, but the generated type sometimes is a unification variable.
/// This helper ensures that such unification variables are not left unresolved, which would lead to an error
let close_equality_typ' (t:term) : Tac unit =
let f = term_as_formula' t in
match f with
| Comp (Eq (Some u)) l _ -> if is_uvar u then (unshelve u; exact_with_ref (tc (cur_env()) l))
| _ -> ()
/// Recursively closing equality types in a given term (usually a unification constraint)
let close_equality_typ (t:term) : Tac unit =
visit_tm close_equality_typ' t
/// Core unification tactic.
/// Transforms terms into their atom representations,
/// Tries to find a solution to AC-unification, and if so,
/// soundly permutes the atom representations before calling the unifier
/// to check the validity of the provided solution.
/// In the case where SMT rewriting was needed, equalities abduction is performed by instantiating the
/// abduction prop unification variable with the corresponding guard
/// 09/24:
///
/// The tactic internally builds a map from atoms to terms
/// and uses the map for reflecting the goal to atoms representation
/// During reflection, the tactics engine typechecks the amap, and hence all
/// the terms again
/// This typechecking of terms is unnecessary, since the terms are coming
/// from the goal, and hence are already well-typed
/// Worse, re-typechecking them may generate a lot of SMT queries
/// And even worse, the SMT queries are discharged in the static context,
/// requiring various workarounds (e.g. squash variables for if conditions etc.)
///
/// To fix this, we now "name" the terms and use the amap with names
///
/// Read through the canon_l_r function for how we do this
/// The following three lemmas are helpers to manipulate the goal in canon_l_r
[@@ no_subtyping]
let inst_bv (#a:Type) (#p:a -> Type0) (#q:Type0) (x:a) (_:squash (p x ==> q))
: Lemma ((forall (x:a). p x) ==> q) = ()
let modus_ponens (#p #q:Type0) (_:squash p)
: Lemma ((p ==> q) ==> q)
= ()
let cut (p q:Type0) : Lemma (requires p /\ (p ==> q)) (ensures q) = ()
let and_true (p: Type0) : Lemma (requires (p /\ (p ==> True))) (ensures p) = ()
let solve_implies_true (p: Type0) : Lemma (p ==> True) = ()
// This exception is raised for failures that should not be considered
// hard but should allow postponing the goal instead
exception Postpone of string
(* NOTE! Redefining boolean disjunction to *not* be short-circuiting,
since we cannot use an effectful result as argument of Prims.op_BarBar *) | {
"checked_file": "/",
"dependencies": [
"Steel.Memory.fsti.checked",
"prims.fst.checked",
"FStar.Tactics.V2.fst.checked",
"FStar.Tactics.CanonCommMonoidSimple.Equiv.fst.checked",
"FStar.String.fsti.checked",
"FStar.Squash.fsti.checked",
"FStar.Set.fsti.checked",
"FStar.Reflection.V2.Derived.Lemmas.fst.checked",
"FStar.Pervasives.Native.fst.checked",
"FStar.Pervasives.fsti.checked",
"FStar.List.Tot.Base.fst.checked",
"FStar.List.Tot.fst.checked",
"FStar.Ghost.fsti.checked",
"FStar.FunctionalExtensionality.fsti.checked",
"FStar.Classical.fsti.checked",
"FStar.Algebra.CommMonoid.Equiv.fst.checked"
],
"interface_file": false,
"source_file": "Steel.Effect.Common.fsti"
} | [
{
"abbrev": false,
"full_module": "FStar.Reflection.V2.Derived.Lemmas",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.Algebra.CommMonoid.Equiv",
"short_module": "CE"
},
{
"abbrev": false,
"full_module": "FStar.Tactics.CanonCommMonoidSimple.Equiv",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Tactics.V2",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": false,
"full_module": "Steel.Semantics.Instantiate",
"short_module": null
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": true,
"full_module": "Steel.Semantics.Hoare.MST",
"short_module": "Sem"
},
{
"abbrev": true,
"full_module": "FStar.Tactics.V2",
"short_module": "T"
},
{
"abbrev": false,
"full_module": "FStar.Ghost",
"short_module": null
},
{
"abbrev": true,
"full_module": "FStar.FunctionalExtensionality",
"short_module": "FExt"
},
{
"abbrev": true,
"full_module": "Steel.Memory",
"short_module": "Mem"
},
{
"abbrev": false,
"full_module": "Steel.Memory",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "Steel.Effect",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar.Pervasives",
"short_module": null
},
{
"abbrev": false,
"full_module": "Prims",
"short_module": null
},
{
"abbrev": false,
"full_module": "FStar",
"short_module": null
}
] | {
"detail_errors": false,
"detail_hint_replay": false,
"initial_fuel": 2,
"initial_ifuel": 1,
"max_fuel": 8,
"max_ifuel": 2,
"no_plugins": false,
"no_smt": false,
"no_tactics": false,
"quake_hi": 1,
"quake_keep": false,
"quake_lo": 1,
"retry": false,
"reuse_hint_for": null,
"smtencoding_elim_box": false,
"smtencoding_l_arith_repr": "boxwrap",
"smtencoding_nl_arith_repr": "boxwrap",
"smtencoding_valid_elim": false,
"smtencoding_valid_intro": true,
"tcnorm": true,
"trivial_pre_for_unannotated_effectful_fns": true,
"z3cliopt": [],
"z3refresh": false,
"z3rlimit": 5,
"z3rlimit_factor": 1,
"z3seed": 0,
"z3smtopt": [],
"z3version": "4.8.5"
} | false | _: Prims.bool -> _: Prims.bool -> Prims.bool | Prims.Tot | [
"total"
] | [] | [
"Prims.op_BarBar"
] | [] | false | false | false | true | false | let bor =
| op_BarBar | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.