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
LowParseWriters.fsti
LowParseWriters.validate_post
val validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b))
val validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b))
let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 47, "end_line": 785, "start_col": 0, "start_line": 778 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.LowParse.parser -> inv: LowParseWriters.memory_invariant -> b: LowStar.Buffer.buffer FStar.UInt8.t -> LowParseWriters.pure_post' (LowParseWriters.ptr p inv * FStar.UInt32.t) (LowParseWriters.validate_pre inv b)
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.memory_invariant", "LowStar.Buffer.buffer", "FStar.UInt8.t", "FStar.Pervasives.Native.tuple2", "LowParseWriters.ptr", "FStar.UInt32.t", "Prims.l_and", "LowParseWriters.LowParse.valid_pos", "FStar.Ghost.reveal", "FStar.Monotonic.HyperStack.mem", "LowParseWriters.__proj__Mkmemory_invariant__item__h0", "FStar.UInt32.__uint_to_t", "Prims.eq2", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.deref_spec", "LowParseWriters.LowParse.contents", "LowParseWriters.pure_post'", "LowParseWriters.validate_pre" ]
[]
false
false
false
false
false
let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) =
fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos
false
LowParseWriters.fsti
LowParseWriters.lift_read
val lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) (fun st x st' -> st == st' /\ post x) (fun _ -> post_err ()) inv)
val lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) (fun st x st' -> st == st' /\ post x) (fun _ -> post_err ()) inv)
let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 116, "end_line": 1259, "start_col": 0, "start_line": 1245 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> pre: Prims.pure_pre -> post: LowParseWriters.pure_post' a pre -> post_err: LowParseWriters.pure_post_err pre -> inv: LowParseWriters.memory_invariant -> r: LowParseWriters.LowParse.parser -> f_read_spec: LowParseWriters.read_repr a pre post post_err inv -> LowParseWriters.repr a r r (fun _ -> pre) (fun st x st' -> st == st' /\ post x) (fun _ -> post_err ()) inv
Prims.Tot
[ "total" ]
[]
[ "Prims.pure_pre", "LowParseWriters.pure_post'", "LowParseWriters.pure_post_err", "LowParseWriters.memory_invariant", "LowParseWriters.LowParse.parser", "LowParseWriters.read_repr", "LowParseWriters.Repr", "LowParseWriters.LowParse.__proj__Parser__item__t", "Prims.l_and", "Prims.eq2", "LowParseWriters.lift_read_spec", "LowParseWriters.lift_read_impl", "LowParseWriters.repr" ]
[]
false
false
false
false
false
let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) (fun st x st' -> st == st' /\ post x) (fun _ -> post_err ()) inv) =
Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec)
false
LowParseWriters.fsti
LowParseWriters.deref_repr
val deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv)
val deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv)
let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 61, "end_line": 715, "start_col": 0, "start_line": 709 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x)))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.LowParse.leaf_reader p -> x: LowParseWriters.ptr p inv -> LowParseWriters.read_repr (Parser?.t p) Prims.l_True (fun res -> res == LowParseWriters.deref_spec x) (fun _ -> Prims.l_False) inv
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.memory_invariant", "LowParseWriters.LowParse.leaf_reader", "LowParseWriters.ptr", "LowParseWriters.ReadRepr", "LowParseWriters.LowParse.__proj__Parser__item__t", "Prims.l_True", "Prims.eq2", "LowParseWriters.deref_spec", "Prims.unit", "Prims.l_False", "LowParseWriters.Correct", "LowParseWriters.result", "LowParseWriters.deref_impl", "LowParseWriters.read_repr" ]
[]
false
false
false
false
false
let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) =
ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x)
false
LowParseWriters.fsti
LowParseWriters.access_repr
val access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv)
val access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv)
let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 65, "end_line": 755, "start_col": 0, "start_line": 747 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x)))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.LowParse.accessor g -> x: LowParseWriters.ptr p1 inv -> LowParseWriters.read_repr (LowParseWriters.ptr p2 inv) (Mkclens?.clens_cond lens (LowParseWriters.deref_spec x)) (fun res -> Mkclens?.clens_cond lens (LowParseWriters.deref_spec x) /\ LowParseWriters.deref_spec res == Mkclens?.clens_get lens (LowParseWriters.deref_spec x)) (fun _ -> Prims.l_False) inv
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.LowParse.clens", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.memory_invariant", "LowParseWriters.LowParse.gaccessor", "LowParseWriters.LowParse.accessor", "LowParseWriters.ptr", "LowParseWriters.ReadRepr", "LowParseWriters.LowParse.__proj__Mkclens__item__clens_cond", "LowParseWriters.deref_spec", "Prims.l_and", "Prims.eq2", "LowParseWriters.LowParse.__proj__Mkclens__item__clens_get", "Prims.unit", "Prims.l_False", "LowParseWriters.Correct", "LowParseWriters.access_spec", "LowParseWriters.result", "LowParseWriters.access_impl", "LowParseWriters.read_repr" ]
[]
false
false
false
false
false
let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) =
ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x)
false
LowParseWriters.fsti
LowParseWriters.test_read_if
val test_read_if (inv: memory_invariant) (f: (unit -> Read bool (True) (fun _ -> True) inv)) : Read bool (True) (fun _ -> True) inv
val test_read_if (inv: memory_invariant) (f: (unit -> Read bool (True) (fun _ -> True) inv)) : Read bool (True) (fun _ -> True) inv
let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 12, "end_line": 493, "start_col": 0, "start_line": 487 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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
inv: LowParseWriters.memory_invariant -> f: (_: Prims.unit -> LowParseWriters.Read Prims.bool) -> LowParseWriters.Read Prims.bool
LowParseWriters.Read
[]
[]
[ "LowParseWriters.memory_invariant", "Prims.unit", "Prims.bool", "Prims.l_True" ]
[]
false
true
false
false
false
let test_read_if (inv: memory_invariant) (f: (unit -> Read bool (True) (fun _ -> True) inv)) : Read bool (True) (fun _ -> True) inv =
if f () then false else false
false
LowParseWriters.fsti
LowParseWriters.destr_repr_spec
val destr_repr_spec (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot (repr_spec a r_in r_out pre post post_err)
val destr_repr_spec (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot (repr_spec a r_in r_out pre post post_err)
let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ()))
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 38, "end_line": 1298, "start_col": 0, "start_line": 1288 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> r_in: LowParseWriters.LowParse.parser -> r_out: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t r_in -> post: LowParseWriters.post_t a r_in r_out pre -> post_err: LowParseWriters.post_err_t r_in pre -> l: LowParseWriters.memory_invariant -> $f_destr_spec: (_: Prims.unit -> LowParseWriters.EWrite a) -> LowParseWriters.repr_spec a r_in r_out pre post post_err
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.memory_invariant", "Prims.unit", "LowParseWriters.__proj__Repr__item__spec", "LowParseWriters.repr_spec" ]
[]
false
false
false
false
false
let destr_repr_spec (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot (repr_spec a r_in r_out pre post post_err) =
Repr?.spec (reify (f_destr_spec ()))
false
LowParseWriters.fsti
LowParseWriters.destr_repr_impl
val destr_repr_impl (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec))
val destr_repr_impl (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec))
let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ()))
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 38, "end_line": 1311, "start_col": 0, "start_line": 1301 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ()))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> r_in: LowParseWriters.LowParse.parser -> r_out: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t r_in -> post: LowParseWriters.post_t a r_in r_out pre -> post_err: LowParseWriters.post_err_t r_in pre -> l: LowParseWriters.memory_invariant -> $f_destr_spec: (_: Prims.unit -> LowParseWriters.EWrite a) -> LowParseWriters.repr_impl a r_in r_out pre post post_err l (LowParseWriters.destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.memory_invariant", "Prims.unit", "LowParseWriters.__proj__Repr__item__impl", "LowParseWriters.repr_impl", "LowParseWriters.destr_repr_spec" ]
[]
false
false
false
false
false
let destr_repr_impl (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) =
Repr?.impl (reify (f_destr_spec ()))
false
LowParseWriters.fsti
LowParseWriters.validate
val validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t{B.len b == len}) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv
val validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t{B.len b == len}) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv
let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 44, "end_line": 828, "start_col": 0, "start_line": 821 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.LowParse.validator p -> inv: LowParseWriters.memory_invariant -> b: LowStar.Buffer.buffer FStar.UInt8.t -> len: FStar.UInt32.t{LowStar.Monotonic.Buffer.len b == len} -> LowParseWriters.ERead (LowParseWriters.ptr p inv * FStar.UInt32.t)
LowParseWriters.ERead
[]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.LowParse.validator", "LowParseWriters.memory_invariant", "LowStar.Buffer.buffer", "FStar.UInt8.t", "FStar.UInt32.t", "Prims.eq2", "LowStar.Monotonic.Buffer.len", "LowStar.Buffer.trivial_preorder", "LowParseWriters.validate_repr", "FStar.Pervasives.Native.tuple2", "LowParseWriters.ptr", "LowParseWriters.validate_pre", "LowParseWriters.validate_post", "LowParseWriters.validate_post_err" ]
[]
false
true
false
false
false
let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t{B.len b == len}) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv =
ERead?.reflect (validate_repr v inv b len)
false
LowParseWriters.fsti
LowParseWriters.extract
val extract (#a: Type u#x) (#r_in #r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot (extract_t l f_destr_spec)
val extract (#a: Type u#x) (#r_in #r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot (extract_t l f_destr_spec)
let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 80, "end_line": 1368, "start_col": 0, "start_line": 1358 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end )))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.memory_invariant -> $f_destr_spec: (_: Prims.unit -> LowParseWriters.EWrite a) -> LowParseWriters.extract_t l f_destr_spec
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.memory_invariant", "Prims.unit", "LowParseWriters.extract_repr_impl", "LowParseWriters.destr_repr_spec", "LowParseWriters.destr_repr_impl", "LowParseWriters.extract_t" ]
[]
false
false
false
false
false
let extract (#a: Type u#x) (#r_in #r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot (extract_t l f_destr_spec) =
extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec)
false
LowParseWriters.fsti
LowParseWriters.buffer_sub
val buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) (B.live inv.h0 b /\ (B.loc_buffer b) `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ (B.loc_buffer res) `B.loc_disjoint` inv.lwrite) inv
val buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) (B.live inv.h0 b /\ (B.loc_buffer b) `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ (B.loc_buffer res) `B.loc_disjoint` inv.lwrite) inv
let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len))
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 59, "end_line": 650, "start_col": 0, "start_line": 632 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowStar.Buffer.buffer t -> i: FStar.UInt32.t -> len: FStar.Ghost.erased FStar.UInt32.t -> LowParseWriters.Read (LowStar.Buffer.buffer t)
LowParseWriters.Read
[]
[]
[ "LowParseWriters.memory_invariant", "LowStar.Buffer.buffer", "FStar.UInt32.t", "FStar.Ghost.erased", "LowParseWriters.ReadRepr", "Prims.l_and", "LowStar.Monotonic.Buffer.live", "LowStar.Buffer.trivial_preorder", "FStar.Ghost.reveal", "FStar.Monotonic.HyperStack.mem", "LowParseWriters.__proj__Mkmemory_invariant__item__h0", "LowStar.Monotonic.Buffer.loc_disjoint", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Monotonic.Buffer.loc", "LowParseWriters.__proj__Mkmemory_invariant__item__lwrite", "Prims.b2t", "Prims.op_LessThanOrEqual", "Prims.op_Addition", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.length", "Prims.eq2", "LowStar.Monotonic.Buffer.mbuffer", "LowStar.Buffer.gsub", "Prims.unit", "Prims.l_False", "LowParseWriters.buffer_sub_spec", "LowParseWriters.buffer_sub_impl" ]
[]
false
true
false
false
false
let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) (B.live inv.h0 b /\ (B.loc_buffer b) `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ (B.loc_buffer res) `B.loc_disjoint` inv.lwrite) inv =
ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len))
false
LowParseWriters.fsti
LowParseWriters.buffer_index_spec
val buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t (B.live inv.h0 b /\ (B.loc_buffer b) `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i)) (fun _ -> False))
val buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t (B.live inv.h0 b /\ (B.loc_buffer b) `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i)) (fun _ -> False))
let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 45, "end_line": 570, "start_col": 0, "start_line": 550 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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
inv: LowParseWriters.memory_invariant -> b: LowStar.Buffer.buffer t -> i: FStar.UInt32.t -> LowParseWriters.read_repr_spec t (LowStar.Monotonic.Buffer.live (FStar.Ghost.reveal (Mkmemory_invariant?.h0 inv)) b /\ LowStar.Monotonic.Buffer.loc_disjoint (LowStar.Monotonic.Buffer.loc_buffer b) (FStar.Ghost.reveal (Mkmemory_invariant?.lwrite inv)) /\ FStar.UInt32.v i < LowStar.Monotonic.Buffer.length b) (fun res -> FStar.UInt32.v i < LowStar.Monotonic.Buffer.length b /\ res == FStar.Seq.Base.index (LowStar.Monotonic.Buffer.as_seq (FStar.Ghost.reveal (Mkmemory_invariant?.h0 inv)) b) (FStar.UInt32.v i)) (fun _ -> Prims.l_False)
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.memory_invariant", "LowStar.Buffer.buffer", "FStar.UInt32.t", "Prims.unit", "LowParseWriters.Correct", "FStar.Seq.Base.index", "LowStar.Monotonic.Buffer.as_seq", "LowStar.Buffer.trivial_preorder", "FStar.Ghost.reveal", "FStar.Monotonic.HyperStack.mem", "LowParseWriters.__proj__Mkmemory_invariant__item__h0", "FStar.UInt.uint_t", "FStar.UInt32.v", "LowParseWriters.result", "LowParseWriters.read_repr_spec", "Prims.l_and", "LowStar.Monotonic.Buffer.live", "LowStar.Monotonic.Buffer.loc_disjoint", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Monotonic.Buffer.loc", "LowParseWriters.__proj__Mkmemory_invariant__item__lwrite", "Prims.b2t", "Prims.op_LessThan", "LowStar.Monotonic.Buffer.length", "Prims.eq2", "Prims.l_False" ]
[]
false
false
false
false
false
let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t (B.live inv.h0 b /\ (B.loc_buffer b) `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i)) (fun _ -> False)) =
fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j)
false
LowParseWriters.fsti
LowParseWriters.buffer_index
val buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t (B.live inv.h0 b /\ (B.loc_buffer b) `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i)) inv
val buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t (B.live inv.h0 b /\ (B.loc_buffer b) `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i)) inv
let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i))
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 57, "end_line": 597, "start_col": 0, "start_line": 581 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowStar.Buffer.buffer t -> i: FStar.UInt32.t -> LowParseWriters.Read t
LowParseWriters.Read
[]
[]
[ "LowParseWriters.memory_invariant", "LowStar.Buffer.buffer", "FStar.UInt32.t", "LowParseWriters.ReadRepr", "Prims.l_and", "LowStar.Monotonic.Buffer.live", "LowStar.Buffer.trivial_preorder", "FStar.Ghost.reveal", "FStar.Monotonic.HyperStack.mem", "LowParseWriters.__proj__Mkmemory_invariant__item__h0", "LowStar.Monotonic.Buffer.loc_disjoint", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Monotonic.Buffer.loc", "LowParseWriters.__proj__Mkmemory_invariant__item__lwrite", "Prims.b2t", "Prims.op_LessThan", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.length", "Prims.eq2", "FStar.Seq.Base.index", "LowStar.Monotonic.Buffer.as_seq", "Prims.unit", "Prims.l_False", "LowParseWriters.buffer_index_spec", "LowParseWriters.buffer_index_impl" ]
[]
false
true
false
false
false
let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t (B.live inv.h0 b /\ (B.loc_buffer b) `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i)) inv =
ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i))
false
LowParseWriters.fsti
LowParseWriters.access
val access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv
val access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv
let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 33, "end_line": 766, "start_col": 0, "start_line": 758 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.LowParse.accessor g -> x: LowParseWriters.ptr p1 inv -> LowParseWriters.Read (LowParseWriters.ptr p2 inv)
LowParseWriters.Read
[]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.LowParse.clens", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.memory_invariant", "LowParseWriters.LowParse.gaccessor", "LowParseWriters.LowParse.accessor", "LowParseWriters.ptr", "LowParseWriters.access_repr", "LowParseWriters.LowParse.__proj__Mkclens__item__clens_cond", "LowParseWriters.deref_spec", "Prims.l_and", "Prims.eq2", "LowParseWriters.LowParse.__proj__Mkclens__item__clens_get" ]
[]
false
true
false
false
false
let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv =
Read?.reflect (access_repr a x)
false
LowParseWriters.fsti
LowParseWriters.frame_out
val frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser
val frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser
let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 22, "end_line": 1427, "start_col": 0, "start_line": 1422 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> frame: LowParseWriters.LowParse.parser -> p: LowParseWriters.LowParse.parser -> LowParseWriters.LowParse.parser
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.LowParse.parse_pair" ]
[]
false
false
false
true
false
let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser =
frame `parse_pair` p
false
Hacl.P256.PrecompTable.fst
Hacl.P256.PrecompTable.precomp_basepoint_table_lemma_w4
val precomp_basepoint_table_lemma_w4: unit -> Lemma (forall (i:nat{i < 16}). precomp_table_acc_inv g_aff 16 precomp_basepoint_table_lseq_w4 i)
val precomp_basepoint_table_lemma_w4: unit -> Lemma (forall (i:nat{i < 16}). precomp_table_acc_inv g_aff 16 precomp_basepoint_table_lseq_w4 i)
let precomp_basepoint_table_lemma_w4 () = normalize_term_spec (SPT.precomp_base_table_list mk_p256_precomp_base_table S.base_point 15); SPT.precomp_base_table_lemma mk_p256_precomp_base_table S.base_point 16 precomp_basepoint_table_lseq_w4
{ "file_name": "code/ecdsap256/Hacl.P256.PrecompTable.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 105, "end_line": 209, "start_col": 0, "start_line": 207 }
module Hacl.P256.PrecompTable open FStar.HyperStack open FStar.HyperStack.ST open FStar.Mul open Lib.IntTypes open Lib.Buffer module ST = FStar.HyperStack.ST module LSeq = Lib.Sequence module LE = Lib.Exponentiation module SE = Spec.Exponentiation module SPT = Hacl.Spec.PrecompBaseTable module SPT256 = Hacl.Spec.PrecompBaseTable256 module SPTK = Hacl.Spec.P256.PrecompTable module S = Spec.P256 module SL = Spec.P256.Lemmas open Hacl.Impl.P256.Point include Hacl.Impl.P256.Group #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" let proj_point_to_list p = SPTK.proj_point_to_list_lemma p; SPTK.proj_point_to_list p let lemma_refl x = SPTK.proj_point_to_list_lemma x //----------------- inline_for_extraction noextract let proj_g_pow2_64 : S.proj_point = [@inline_let] let rX : S.felem = 0x000931f4ae428a4ad81ee0aa89cf5247ce85d4dd696c61b4bb9d4761e57b7fbe in [@inline_let] let rY : S.felem = 0x7e88e5e6a142d5c2269f21a158e82ab2c79fcecb26e397b96fd5b9fbcd0a69a5 in [@inline_let] let rZ : S.felem = 0x02626dc2dd5e06cd19de5e6afb6c5dbdd3e41dc1472e7b8ef11eb0662e41c44b in (rX, rY, rZ) val lemma_proj_g_pow2_64_eval : unit -> Lemma (SE.exp_pow2 S.mk_p256_concrete_ops S.base_point 64 == proj_g_pow2_64) let lemma_proj_g_pow2_64_eval () = SPT256.exp_pow2_rec_is_exp_pow2 S.mk_p256_concrete_ops S.base_point 64; let qX, qY, qZ = normalize_term (SPT256.exp_pow2_rec S.mk_p256_concrete_ops S.base_point 64) in normalize_term_spec (SPT256.exp_pow2_rec S.mk_p256_concrete_ops S.base_point 64); let rX : S.felem = 0x000931f4ae428a4ad81ee0aa89cf5247ce85d4dd696c61b4bb9d4761e57b7fbe in let rY : S.felem = 0x7e88e5e6a142d5c2269f21a158e82ab2c79fcecb26e397b96fd5b9fbcd0a69a5 in let rZ : S.felem = 0x02626dc2dd5e06cd19de5e6afb6c5dbdd3e41dc1472e7b8ef11eb0662e41c44b in assert_norm (qX == rX /\ qY == rY /\ qZ == rZ) inline_for_extraction noextract let proj_g_pow2_128 : S.proj_point = [@inline_let] let rX : S.felem = 0x04c3aaf6c6c00704e96eda89461d63fd2c97ee1e6786fc785e6afac7aa92f9b1 in [@inline_let] let rY : S.felem = 0x14f1edaeb8e9c8d4797d164a3946c7ff50a7c8cd59139a4dbce354e6e4df09c3 in [@inline_let] let rZ : S.felem = 0x80119ced9a5ce83c4e31f8de1a38f89d5f9ff9f637dca86d116a4217f83e55d2 in (rX, rY, rZ) val lemma_proj_g_pow2_128_eval : unit -> Lemma (SE.exp_pow2 S.mk_p256_concrete_ops proj_g_pow2_64 64 == proj_g_pow2_128) let lemma_proj_g_pow2_128_eval () = SPT256.exp_pow2_rec_is_exp_pow2 S.mk_p256_concrete_ops proj_g_pow2_64 64; let qX, qY, qZ = normalize_term (SPT256.exp_pow2_rec S.mk_p256_concrete_ops proj_g_pow2_64 64) in normalize_term_spec (SPT256.exp_pow2_rec S.mk_p256_concrete_ops proj_g_pow2_64 64); let rX : S.felem = 0x04c3aaf6c6c00704e96eda89461d63fd2c97ee1e6786fc785e6afac7aa92f9b1 in let rY : S.felem = 0x14f1edaeb8e9c8d4797d164a3946c7ff50a7c8cd59139a4dbce354e6e4df09c3 in let rZ : S.felem = 0x80119ced9a5ce83c4e31f8de1a38f89d5f9ff9f637dca86d116a4217f83e55d2 in assert_norm (qX == rX /\ qY == rY /\ qZ == rZ) inline_for_extraction noextract let proj_g_pow2_192 : S.proj_point = [@inline_let] let rX : S.felem = 0xc762a9c8ae1b2f7434ff8da70fe105e0d4f188594989f193de0dbdbf5f60cb9a in [@inline_let] let rY : S.felem = 0x1eddaf51836859e1369f1ae8d9ab02e4123b6f151d9b796e297a38fa5613d9bc in [@inline_let] let rZ : S.felem = 0xcb433ab3f67815707e398dc7910cc4ec6ea115360060fc73c35b53dce02e2c72 in (rX, rY, rZ) val lemma_proj_g_pow2_192_eval : unit -> Lemma (SE.exp_pow2 S.mk_p256_concrete_ops proj_g_pow2_128 64 == proj_g_pow2_192) let lemma_proj_g_pow2_192_eval () = SPT256.exp_pow2_rec_is_exp_pow2 S.mk_p256_concrete_ops proj_g_pow2_128 64; let qX, qY, qZ = normalize_term (SPT256.exp_pow2_rec S.mk_p256_concrete_ops proj_g_pow2_128 64) in normalize_term_spec (SPT256.exp_pow2_rec S.mk_p256_concrete_ops proj_g_pow2_128 64); let rX : S.felem = 0xc762a9c8ae1b2f7434ff8da70fe105e0d4f188594989f193de0dbdbf5f60cb9a in let rY : S.felem = 0x1eddaf51836859e1369f1ae8d9ab02e4123b6f151d9b796e297a38fa5613d9bc in let rZ : S.felem = 0xcb433ab3f67815707e398dc7910cc4ec6ea115360060fc73c35b53dce02e2c72 in assert_norm (qX == rX /\ qY == rY /\ qZ == rZ) // let proj_g_pow2_64 : S.proj_point = // normalize_term (SPT256.exp_pow2_rec S.mk_p256_concrete_ops S.base_point 64) // let proj_g_pow2_128 : S.proj_point = // normalize_term (SPT256.exp_pow2_rec S.mk_p256_concrete_ops proj_g_pow2_64 64) // let proj_g_pow2_192 : S.proj_point = // normalize_term (SPT256.exp_pow2_rec S.mk_p256_concrete_ops proj_g_pow2_128 64) inline_for_extraction noextract let proj_g_pow2_64_list : SPTK.point_list = normalize_term (SPTK.proj_point_to_list proj_g_pow2_64) inline_for_extraction noextract let proj_g_pow2_128_list : SPTK.point_list = normalize_term (SPTK.proj_point_to_list proj_g_pow2_128) inline_for_extraction noextract let proj_g_pow2_192_list : SPTK.point_list = normalize_term (SPTK.proj_point_to_list proj_g_pow2_192) let proj_g_pow2_64_lseq : LSeq.lseq uint64 12 = normalize_term_spec (SPTK.proj_point_to_list proj_g_pow2_64); Seq.seq_of_list proj_g_pow2_64_list let proj_g_pow2_128_lseq : LSeq.lseq uint64 12 = normalize_term_spec (SPTK.proj_point_to_list proj_g_pow2_128); Seq.seq_of_list proj_g_pow2_128_list let proj_g_pow2_192_lseq : LSeq.lseq uint64 12 = normalize_term_spec (SPTK.proj_point_to_list proj_g_pow2_192); Seq.seq_of_list proj_g_pow2_192_list val proj_g_pow2_64_lemma: unit -> Lemma (S.to_aff_point proj_g_pow2_64 == pow_point (pow2 64) g_aff) let proj_g_pow2_64_lemma () = lemma_proj_g_pow2_64_eval (); SPT256.a_pow2_64_lemma S.mk_p256_concrete_ops S.base_point val proj_g_pow2_128_lemma: unit -> Lemma (S.to_aff_point proj_g_pow2_128 == pow_point (pow2 128) g_aff) let proj_g_pow2_128_lemma () = lemma_proj_g_pow2_128_eval (); lemma_proj_g_pow2_64_eval (); SPT256.a_pow2_128_lemma S.mk_p256_concrete_ops S.base_point val proj_g_pow2_192_lemma: unit -> Lemma (S.to_aff_point proj_g_pow2_192 == pow_point (pow2 192) g_aff) let proj_g_pow2_192_lemma () = lemma_proj_g_pow2_192_eval (); lemma_proj_g_pow2_128_eval (); lemma_proj_g_pow2_64_eval (); SPT256.a_pow2_192_lemma S.mk_p256_concrete_ops S.base_point let proj_g_pow2_64_lseq_lemma () = normalize_term_spec (SPTK.proj_point_to_list proj_g_pow2_64); proj_g_pow2_64_lemma (); SPTK.proj_point_to_list_lemma proj_g_pow2_64 let proj_g_pow2_128_lseq_lemma () = normalize_term_spec (SPTK.proj_point_to_list proj_g_pow2_128); proj_g_pow2_128_lemma (); SPTK.proj_point_to_list_lemma proj_g_pow2_128 let proj_g_pow2_192_lseq_lemma () = normalize_term_spec (SPTK.proj_point_to_list proj_g_pow2_192); proj_g_pow2_192_lemma (); SPTK.proj_point_to_list_lemma proj_g_pow2_192 let mk_proj_g_pow2_64 () = createL proj_g_pow2_64_list let mk_proj_g_pow2_128 () = createL proj_g_pow2_128_list let mk_proj_g_pow2_192 () = createL proj_g_pow2_192_list //---------------- /// window size = 4; precomputed table = [[0]G, [1]G, ..., [15]G] inline_for_extraction noextract let precomp_basepoint_table_list_w4: x:list uint64{FStar.List.Tot.length x = 192} = normalize_term (SPT.precomp_base_table_list mk_p256_precomp_base_table S.base_point 15) let precomp_basepoint_table_lseq_w4 : LSeq.lseq uint64 192 = normalize_term_spec (SPT.precomp_base_table_list mk_p256_precomp_base_table S.base_point 15); Seq.seq_of_list precomp_basepoint_table_list_w4
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "Spec.Exponentiation.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Exponentiation.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.PrecompBaseTable256.fsti.checked", "Hacl.Spec.PrecompBaseTable.fsti.checked", "Hacl.Spec.P256.PrecompTable.fsti.checked", "Hacl.Impl.P256.Point.fsti.checked", "Hacl.Impl.P256.Group.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked" ], "interface_file": true, "source_file": "Hacl.P256.PrecompTable.fst" }
[ { "abbrev": true, "full_module": "Spec.P256.Lemmas", "short_module": "SL" }, { "abbrev": true, "full_module": "Hacl.Spec.P256.PrecompTable", "short_module": "SPTK" }, { "abbrev": true, "full_module": "Hacl.Spec.PrecompBaseTable256", "short_module": "SPT256" }, { "abbrev": true, "full_module": "Lib.Exponentiation", "short_module": "LE" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Group", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Point", "short_module": null }, { "abbrev": true, "full_module": "Hacl.Spec.P256.Montgomery", "short_module": "SM" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": true, "full_module": "Hacl.Spec.PrecompBaseTable", "short_module": "SPT" }, { "abbrev": true, "full_module": "Hacl.Impl.Exponentiation.Definitions", "short_module": "BE" }, { "abbrev": true, "full_module": "Spec.Exponentiation", "short_module": "SE" }, { "abbrev": true, "full_module": "Lib.Exponentiation.Definition", "short_module": "LE" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "Hacl.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.P256", "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
_: Prims.unit -> FStar.Pervasives.Lemma (ensures forall (i: Prims.nat{i < 16}). Hacl.P256.PrecompTable.precomp_table_acc_inv Hacl.P256.PrecompTable.g_aff 16 Hacl.P256.PrecompTable.precomp_basepoint_table_lseq_w4 i)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.unit", "Hacl.Spec.PrecompBaseTable.precomp_base_table_lemma", "Spec.P256.PointOps.proj_point", "Lib.IntTypes.U64", "FStar.UInt32.uint_to_t", "Hacl.P256.PrecompTable.mk_p256_precomp_base_table", "Spec.P256.PointOps.base_point", "Hacl.P256.PrecompTable.precomp_basepoint_table_lseq_w4", "FStar.Pervasives.normalize_term_spec", "Prims.list", "Lib.IntTypes.uint_t", "Lib.IntTypes.SEC", "Prims.b2t", "Prims.op_Equality", "Prims.int", "FStar.List.Tot.Base.length", "FStar.Mul.op_Star", "Prims.op_Addition", "Lib.IntTypes.v", "Lib.IntTypes.U32", "Lib.IntTypes.PUB", "Hacl.Spec.PrecompBaseTable.precomp_base_table_list" ]
[]
true
false
true
false
false
let precomp_basepoint_table_lemma_w4 () =
normalize_term_spec (SPT.precomp_base_table_list mk_p256_precomp_base_table S.base_point 15); SPT.precomp_base_table_lemma mk_p256_precomp_base_table S.base_point 16 precomp_basepoint_table_lseq_w4
false
LowParseWriters.fsti
LowParseWriters.repr_spec
val repr_spec (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x)
val repr_spec (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x)
let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in ))
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 4, "end_line": 859, "start_col": 0, "start_line": 851 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> r_in: LowParseWriters.LowParse.parser -> r_out: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t r_in -> post: LowParseWriters.post_t a r_in r_out pre -> post_err: LowParseWriters.post_err_t r_in pre -> Type
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.result", "FStar.Pervasives.Native.tuple2", "Prims.l_and", "Prims.b2t", "Prims.op_LessThanOrEqual", "LowParseWriters.LowParse.size", "Prims.string" ]
[]
false
false
false
false
true
let repr_spec (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) =
v_in: Parser?.t r_in -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (function | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in))
false
LowParseWriters.fsti
LowParseWriters.repr_impl_post
val repr_impl_post (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 {l.lwrite `B.loc_includes` (B.loc_buffer b)}) (pos1: U32.t{U32.v pos1 <= B.length b}) (h: HS.mem) (res: iresult a) (h': HS.mem) : GTot Type0
val repr_impl_post (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 {l.lwrite `B.loc_includes` (B.loc_buffer b)}) (pos1: U32.t{U32.v pos1 <= B.length b}) (h: HS.mem) (res: iresult a) (h': HS.mem) : GTot Type0
let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end )
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 903, "start_col": 0, "start_line": 868 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> r_in: LowParseWriters.LowParse.parser -> r_out: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t r_in -> post: LowParseWriters.post_t a r_in r_out pre -> post_err: LowParseWriters.post_err_t r_in pre -> l: LowParseWriters.memory_invariant -> spec: LowParseWriters.repr_spec a r_in r_out pre post post_err -> b: LowStar.Buffer.buffer LowParseWriters.LowParse.u8 { LowStar.Monotonic.Buffer.loc_includes (FStar.Ghost.reveal (Mkmemory_invariant?.lwrite l)) (LowStar.Monotonic.Buffer.loc_buffer b) } -> pos1: FStar.UInt32.t{FStar.UInt32.v pos1 <= LowStar.Monotonic.Buffer.length b} -> h: FStar.Monotonic.HyperStack.mem -> res: LowParseWriters.iresult a -> h': FStar.Monotonic.HyperStack.mem -> Prims.GTot Type0
Prims.GTot
[ "sometrivial" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.memory_invariant", "LowParseWriters.repr_spec", "LowStar.Buffer.buffer", "LowParseWriters.LowParse.u8", "LowStar.Monotonic.Buffer.loc_includes", "FStar.Ghost.reveal", "LowStar.Monotonic.Buffer.loc", "LowParseWriters.__proj__Mkmemory_invariant__item__lwrite", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Buffer.trivial_preorder", "FStar.UInt32.t", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.UInt32.v", "LowStar.Monotonic.Buffer.length", "FStar.Monotonic.HyperStack.mem", "LowParseWriters.iresult", "Prims.l_and", "LowParseWriters.LowParse.valid_pos", "FStar.UInt32.__uint_to_t", "LowStar.Monotonic.Buffer.modifies", "FStar.Pervasives.Native.Mktuple2", "LowParseWriters.result", "FStar.Pervasives.Native.tuple2", "LowParseWriters.LowParse.__proj__Parser__item__t", "Prims.eq2", "LowParseWriters.LowParse.contents", "Prims.op_GreaterThan", "LowParseWriters.LowParse.size", "Prims.string", "Prims.l_True", "Prims.l_False", "Prims.logical" ]
[]
false
false
false
false
true
let repr_impl_post (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 {l.lwrite `B.loc_includes` (B.loc_buffer b)}) (pos1: U32.t{U32.v pos1 <= B.length b}) (h: HS.mem) (res: iresult a) (h': HS.mem) : GTot Type0 =
valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ (let v_in = contents r_in h b 0ul pos1 in pre v_in /\ (match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> True | _ -> False))
false
LowParseWriters.fsti
LowParseWriters.frame_post_err
val frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre))
val frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre))
let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err ()
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 22, "end_line": 1452, "start_col": 0, "start_line": 1447 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t LowParseWriters.LowParse.parse_empty -> post_err: LowParseWriters.post_err_t LowParseWriters.LowParse.parse_empty pre -> LowParseWriters.post_err_t frame (LowParseWriters.frame_pre frame pre)
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.LowParse.parse_empty", "LowParseWriters.post_err_t", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.frame_pre" ]
[]
false
false
false
false
false
let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) =
fun _ -> post_err ()
false
LowParseWriters.fsti
LowParseWriters.frame_post
val frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre))
val frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre))
let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 63, "end_line": 1444, "start_col": 0, "start_line": 1437 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> frame: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t LowParseWriters.LowParse.parse_empty -> p: LowParseWriters.LowParse.parser -> post: LowParseWriters.post_t a LowParseWriters.LowParse.parse_empty p pre -> LowParseWriters.post_t a frame (LowParseWriters.frame_out a frame p) (LowParseWriters.frame_pre frame pre)
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.LowParse.parse_empty", "LowParseWriters.post_t", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.frame_out", "Prims.l_and", "Prims.eq2", "LowParseWriters.frame_pre" ]
[]
false
false
false
false
false
let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) =
fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out
false
Steel.Effect.Atomic.fst
Steel.Effect.Atomic.intro_vdep
val intro_vdep (#opened:inames) (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) : SteelGhost unit opened (v `star` q) (fun _ -> vdep v p) (requires (fun h -> q == p (h v))) (ensures (fun h _ h' -> let x2 = h' (vdep v p) in q == p (h v) /\ dfst x2 == (h v) /\ dsnd x2 == (h q) ))
val intro_vdep (#opened:inames) (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) : SteelGhost unit opened (v `star` q) (fun _ -> vdep v p) (requires (fun h -> q == p (h v))) (ensures (fun h _ h' -> let x2 = h' (vdep v p) in q == p (h v) /\ dfst x2 == (h v) /\ dsnd x2 == (h q) ))
let intro_vdep v q p = reveal_star v q; change_slprop_rel_with_cond (v `star` q) (vdep v p) (vdep_cond v q p) (vdep_rel v q p) (fun m -> intro_vdep_lemma v q p m)
{ "file_name": "lib/steel/Steel.Effect.Atomic.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 39, "end_line": 770, "start_col": 0, "start_line": 761 }
(* 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.Atomic open Steel.Effect friend Steel.Effect #set-options "--warn_error -330" //turn off the experimental feature warning let _ : squash (forall (pre:pre_t) (m0:mem{interp (hp_of pre) m0}) (m1:mem{disjoint m0 m1}). mk_rmem pre m0 == mk_rmem pre (join m0 m1)) = Classical.forall_intro rmem_depends_only_on let req_to_act_req (#pre:vprop) (req:req_t pre) : mprop (hp_of pre) = fun m -> rmem_depends_only_on pre; interp (hp_of pre) m /\ req (mk_rmem pre m) unfold let to_post (#a:Type) (post:post_t a) = fun x -> (hp_of (post x)) let ens_to_act_ens (#pre:pre_t) (#a:Type) (#post:post_t a) (ens:ens_t pre a post) : mprop2 (hp_of pre) (to_post post) = fun m0 x m1 -> interp (hp_of pre) m0 /\ interp (hp_of (post x)) m1 /\ ens (mk_rmem pre m0) x (mk_rmem (post x) m1) let repr a framed opened f pre post req ens = action_except_full a opened (hp_of pre) (to_post post) (req_to_act_req req) (ens_to_act_ens ens) let return_ a x opened #p = fun _ -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (p x) (core_mem m0) in lemma_frame_equalities_refl (p x) h0; x #push-options "--fuel 0 --ifuel 0" #push-options "--z3rlimit 20 --fuel 1 --ifuel 1" val frame00 (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:repr a framed opened obs pre post req ens) (frame:vprop) : repr a true opened obs (pre `star` frame) (fun x -> post x `star` frame) (fun h -> req (focus_rmem h pre)) (fun h0 r h1 -> req (focus_rmem h0 pre) /\ ens (focus_rmem h0 pre) r (focus_rmem h1 (post r)) /\ frame_opaque frame (focus_rmem h0 frame) (focus_rmem h1 frame)) module Sem = Steel.Semantics.Hoare.MST module Mem = Steel.Memory let equiv_middle_left_assoc (a b c d:slprop) : Lemma (((a `Mem.star` b) `Mem.star` c `Mem.star` d) `Mem.equiv` (a `Mem.star` (b `Mem.star` c) `Mem.star` d)) = let open Steel.Memory in star_associative a b c; star_congruence ((a `star` b) `star` c) d (a `star` (b `star` c)) d let frame00 #a #framed #opened #obs #pre #post #req #ens f frame = fun frame' -> let m0:full_mem = NMSTTotal.get () in let snap:rmem frame = mk_rmem frame (core_mem m0) in // Need to define it with type annotation, although unused, for it to trigger // the pattern on the framed ensures in the def of MstTot let aux:mprop (hp_of frame `Mem.star` frame') = req_frame frame snap in focus_is_restrict_mk_rmem (pre `star` frame) pre (core_mem m0); assert (interp (hp_of (pre `star` frame) `Mem.star` frame' `Mem.star` locks_invariant opened m0) m0); equiv_middle_left_assoc (hp_of pre) (hp_of frame) frame' (locks_invariant opened m0); assert (interp (hp_of pre `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m0) m0); let x = f (hp_of frame `Mem.star` frame') in let m1:full_mem = NMSTTotal.get () in assert (interp (hp_of (post x) `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m1) m1); equiv_middle_left_assoc (hp_of (post x)) (hp_of frame) frame' (locks_invariant opened m1); assert (interp ((hp_of (post x) `Mem.star` hp_of frame) `Mem.star` frame' `Mem.star` locks_invariant opened m1) m1); focus_is_restrict_mk_rmem (pre `star` frame) frame (core_mem m0); focus_is_restrict_mk_rmem (post x `star` frame) frame (core_mem m1); let h0:rmem (pre `star` frame) = mk_rmem (pre `star` frame) (core_mem m0) in let h1:rmem (post x `star` frame) = mk_rmem (post x `star` frame) (core_mem m1) in assert (focus_rmem h0 frame == focus_rmem h1 frame); focus_is_restrict_mk_rmem (post x `star` frame) (post x) (core_mem m1); lemma_frame_opaque_refl frame (focus_rmem h0 frame); x unfold let bind_req_opaque (#a:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#pr:a -> prop) (req_g:(x:a -> req_t (pre_g x))) (frame_f:vprop) (frame_g:a -> vprop) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) : req_t (pre_f `star` frame_f) = fun m0 -> req_f (focus_rmem m0 pre_f) /\ (forall (x:a) (h1:hmem (post_f x `star` frame_f)). (ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f)) ==> pr x /\ (can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); (req_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)))) unfold let bind_ens_opaque (#a:Type) (#b:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#pr:a -> prop) (ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (frame_f:vprop) (frame_g:a -> vprop) (post:post_t b) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (_:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) : ens_t (pre_f `star` frame_f) b post = fun m0 y m2 -> req_f (focus_rmem m0 pre_f) /\ (exists (x:a) (h1:hmem (post_f x `star` frame_f)). pr x /\ ( can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f) /\ frame_opaque (frame_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (frame_g x)) (focus_rmem m2 (frame_g x)) /\ ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ (ens_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)) y (focus_rmem m2 (post_g x y)))) val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True) #push-options "--z3rlimit 20" let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_f x `star` frame_f) (core_mem m1) in let h1' = mk_rmem (pre_g x `star` frame_g x) (core_mem m1) in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); focus_is_restrict_mk_rmem (post_f x `star` frame_f) (pre_g x `star` frame_g x) (core_mem m1); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of (post_f x `star` frame_f)) (hp_of (pre_g x `star` frame_g x)) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); let h2' = mk_rmem (post_g x y `star` frame_g x) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in // assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem (post_g x y `star` frame_g x) (post y) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of (post_g x y `star` frame_g x)) (hp_of (post y)) frame (locks_invariant opened m2) m2; y let norm_repr (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) (f:repr a framed opened obs pre post req ens) : repr a framed opened obs pre post (fun h -> norm_opaque (req h)) (fun h0 x h1 -> norm_opaque (ens h0 x h1)) = f let bind a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = norm_repr (bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g) val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True) let subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem pre_g (core_mem m0) in can_be_split_trans pre_g (pre_f `star` fr) pre_f; can_be_split_trans pre_g (pre_f `star` fr) fr; can_be_split_3_interp (hp_of pre_g) (hp_of (pre_f `star` fr)) frame (locks_invariant opened m0) m0; focus_replace pre_g (pre_f `star` fr) pre_f (core_mem m0); let x = frame00 f fr frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_g x) (core_mem m1) in let h0' = mk_rmem (pre_f `star` fr) (core_mem m0) in let h1' = mk_rmem (post_f x `star` fr) (core_mem m1) in // From frame00 assert (frame_opaque fr (focus_rmem h0' fr) (focus_rmem h1' fr)); // Replace h0'/h1' by h0/h1 focus_replace pre_g (pre_f `star` fr) fr (core_mem m0); focus_replace (post_g x) (post_f x `star` fr) fr (core_mem m1); assert (frame_opaque fr (focus_rmem h0 fr) (focus_rmem h1 fr)); can_be_split_trans (post_g x) (post_f x `star` fr) (post_f x); can_be_split_trans (post_g x) (post_f x `star` fr) fr; can_be_split_3_interp (hp_of (post_f x `star` fr)) (hp_of (post_g x)) frame (locks_invariant opened m1) m1; focus_replace (post_g x) (post_f x `star` fr) (post_f x) (core_mem m1); x let subcomp a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #_ #pr #p1 #p2 f = lemma_subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2; subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f #pop-options let bind_pure_steela_ a b opened o #wp f g = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun frame -> let x = f () in g x frame let lift_ghost_atomic a o f = f let lift_atomic_steel a o f = f let as_atomic_action f = SteelAtomic?.reflect f let as_atomic_action_ghost f = SteelGhost?.reflect f let as_atomic_unobservable_action f = SteelAtomicU?.reflect f (* Some helpers *) let get0 (#opened:inames) (#p:vprop) (_:unit) : repr (erased (rmem p)) true opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1 /\ frame_equalities p r h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; h0 let get () = SteelGhost?.reflect (get0 ()) let intro_star (p q:vprop) (r:slprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (m:mem) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m) ) : Lemma (requires interp ((hp_of p) `Mem.star` r) m /\ sel_of p m == reveal vp) (ensures interp ((hp_of q) `Mem.star` r) m) = let p = hp_of p in let q = hp_of q in let intro (ml mr:mem) : Lemma (requires interp q ml /\ interp r mr /\ disjoint ml mr) (ensures disjoint ml mr /\ interp (q `Mem.star` r) (Mem.join ml mr)) = Mem.intro_star q r ml mr in elim_star p r m; Classical.forall_intro (Classical.move_requires proof); Classical.forall_intro_2 (Classical.move_requires_2 intro) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let rewrite_slprop0 (#opened:inames) (p q:vprop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ _ -> True) = fun frame -> let m:full_mem = NMSTTotal.get () in proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) (sel_of q m) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let rewrite_slprop p q l = SteelGhost?.reflect (rewrite_slprop0 p q l) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop0 (#opened:inames) (p q:vprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun h -> h p == reveal vp) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) vp vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop p q vp vq l = SteelGhost?.reflect (change_slprop0 p q vp vq l) let change_equal_slprop p q = let m = get () in let x : Ghost.erased (t_of p) = hide ((reveal m) p) in let y : Ghost.erased (t_of q) = Ghost.hide (Ghost.reveal x) in change_slprop p q x y (fun _ -> ()) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop_20 (#opened:inames) (p q:vprop) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop_2 p q vq l = SteelGhost?.reflect (change_slprop_20 p q vq l) let change_slprop_rel0 (#opened:inames) (p q:vprop) (rel : normal (t_of p) -> normal (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel p q rel proof = SteelGhost?.reflect (change_slprop_rel0 p q rel proof) let change_slprop_rel_with_cond0 (#opened:inames) (p q:vprop) (cond: t_of p -> prop) (rel : (t_of p) -> (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ cond (sel_of p m)) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun m -> cond (m p)) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel_with_cond p q cond rel proof = SteelGhost?.reflect (change_slprop_rel_with_cond0 p q cond rel proof) let extract_info0 (#opened:inames) (p:vprop) (vp:erased (normal (t_of p))) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> h p == reveal vp) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info p vp fact l = SteelGhost?.reflect (extract_info0 p vp fact l) let extract_info_raw0 (#opened:inames) (p:vprop) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> True) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info_raw p fact l = SteelGhost?.reflect (extract_info_raw0 p fact l) let noop _ = change_slprop_rel emp emp (fun _ _ -> True) (fun _ -> ()) let sladmit _ = SteelGhostF?.reflect (fun _ -> NMSTTotal.nmst_tot_admit ()) let slassert0 (#opened:inames) (p:vprop) : repr unit false opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0 let slassert p = SteelGhost?.reflect (slassert0 p) let drop p = rewrite_slprop p emp (fun m -> emp_unit (hp_of p); affine_star (hp_of p) Mem.emp m; reveal_emp()) let reveal_star0 (#opened:inames) (p1 p2:vprop) : repr unit false opened Unobservable (p1 `star` p2) (fun _ -> p1 `star` p2) (fun _ -> True) (fun h0 _ h1 -> h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 (p1 `star` p2) == (h0 p1, h0 p2) /\ h1 (p1 `star` p2) == (h1 p1, h1 p2) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem let reveal_star p1 p2 = SteelGhost?.reflect (reveal_star0 p1 p2) let reveal_star_30 (#opened:inames) (p1 p2 p3:vprop) : repr unit false opened Unobservable (p1 `star` p2 `star` p3) (fun _ -> p1 `star` p2 `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split (p1 `star` p2 `star` p3) p1 /\ can_be_split (p1 `star` p2 `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 (p1 `star` p2 `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 (p1 `star` p2 `star` p3) == ((h1 p1, h1 p2), h1 p3) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem (p1 `star` p2 `star` p3) (core_mem m) in can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p1; can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p2; reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2 `star` p3); reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2); reveal_mk_rmem (p1 `star` p2 `star` p3) m p3 let reveal_star_3 p1 p2 p3 = SteelGhost?.reflect (reveal_star_30 p1 p2 p3) let intro_pure p = rewrite_slprop emp (pure p) (fun m -> pure_interp p m) let elim_pure_aux #uses (p:prop) : SteelGhostT (_:unit{p}) uses (pure p) (fun _ -> to_vprop Mem.emp) = as_atomic_action_ghost (Steel.Memory.elim_pure #uses p) let elim_pure #uses p = let _ = elim_pure_aux p in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()) let return #a #opened #p x = SteelAtomicBase?.reflect (return_ a x opened #p) let intro_exists #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists x (h_exists_sl' p) m) let intro_exists_erased #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists (Ghost.reveal x) (h_exists_sl' p) m) let witness_exists #a #u #p _ = SteelGhost?.reflect (Steel.Memory.witness_h_exists #u (fun x -> hp_of (p x))) let lift_exists #a #u p = as_atomic_action_ghost (Steel.Memory.lift_h_exists #u (fun x -> hp_of (p x))) let exists_equiv p q = Classical.forall_intro_2 reveal_equiv; h_exists_cong (h_exists_sl' p) (h_exists_sl' q) let exists_cong p q = rewrite_slprop (h_exists p) (h_exists q) (fun m -> reveal_equiv (h_exists p) (h_exists q); exists_equiv p q) let fresh_invariant #uses p ctxt = rewrite_slprop p (to_vprop (hp_of p)) (fun _ -> ()); let i = as_atomic_unobservable_action (fresh_invariant uses (hp_of p) ctxt) in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()); return i let new_invariant #uses p = let i = fresh_invariant #uses p [] in return i (* * AR: SteelAtomic and SteelGhost are not marked reifiable since we intend to run Steel programs natively * However to implement the with_inv combinators we need to reify their thunks to reprs * We could implement it better by having support for reification only in the .fst file * But for now assuming a function *) assume val reify_steel_atomic_comp (#a:Type) (#framed:bool) (#opened_invariants:inames) (#g:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:unit -> SteelAtomicBase a framed opened_invariants g pre post req ens) : repr a framed opened_invariants g pre post req ens [@@warn_on_use "as_unobservable_atomic_action is a trusted primitive"] let as_atomic_o_action (#a:Type u#a) (#opened_invariants:inames) (#fp:slprop) (#fp': a -> slprop) (o:observability) (f:action_except a opened_invariants fp fp') : SteelAtomicBaseT a opened_invariants o (to_vprop fp) (fun x -> to_vprop (fp' x)) = SteelAtomicBaseT?.reflect f let with_invariant #a #fp #fp' #obs #opened #p i f = rewrite_slprop fp (to_vprop (hp_of fp)) (fun _ -> ()); let x = as_atomic_o_action obs (Steel.Memory.with_invariant #a #(hp_of fp) #(fun x -> hp_of (fp' x)) #opened #(hp_of p) i (reify_steel_atomic_comp f)) in rewrite_slprop (to_vprop (hp_of (fp' x))) (fp' x) (fun _ -> ()); return x assume val reify_steel_ghost_comp (#a:Type) (#framed:bool) (#opened_invariants:inames) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:unit -> SteelGhostBase a framed opened_invariants Unobservable pre post req ens) : repr a framed opened_invariants Unobservable pre post req ens let with_invariant_g #a #fp #fp' #opened #p i f = rewrite_slprop fp (to_vprop (hp_of fp)) (fun _ -> ()); let x = as_atomic_unobservable_action (Steel.Memory.with_invariant #a #(hp_of fp) #(fun x -> hp_of (fp' x)) #opened #(hp_of p) i (reify_steel_ghost_comp f)) in rewrite_slprop (to_vprop (hp_of (fp' x))) (fp' x) (fun _ -> ()); return (hide x) let intro_vrefine v p = let m = get () in let x : Ghost.erased (t_of v) = gget v in let x' : Ghost.erased (vrefine_t v p) = Ghost.hide (Ghost.reveal x) in change_slprop v (vrefine v p) x x' (fun m -> interp_vrefine_hp v p m; vrefine_sel_eq v p m ) let elim_vrefine v p = let h = get() in let x : Ghost.erased (vrefine_t v p) = gget (vrefine v p) in let x' : Ghost.erased (t_of v) = Ghost.hide (Ghost.reveal x) in change_slprop (vrefine v p) v x x' (fun m -> interp_vrefine_hp v p m; vrefine_sel_eq v p m ) let vdep_cond (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (x1: t_of (v `star` q)) : Tot prop = q == p (fst x1) let vdep_rel (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (x1: t_of (v `star` q)) (x2: (t_of (vdep v p))) : Tot prop = q == p (fst x1) /\ dfst (x2 <: (dtuple2 (t_of v) (vdep_payload v p))) == fst x1 /\ dsnd (x2 <: (dtuple2 (t_of v) (vdep_payload v p))) == snd x1 let intro_vdep_lemma (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (m: mem) : Lemma (requires ( interp (hp_of (v `star` q)) m /\ q == p (fst (sel_of (v `star` q) m)) )) (ensures ( interp (hp_of (v `star` q)) m /\ interp (hp_of (vdep v p)) m /\ vdep_rel v q p (sel_of (v `star` q) m) (sel_of (vdep v p) m) )) = Mem.interp_star (hp_of v) (hp_of q) m; interp_vdep_hp v p m; vdep_sel_eq v p m
{ "checked_file": "/", "dependencies": [ "Steel.Semantics.Hoare.MST.fst.checked", "Steel.Memory.fsti.checked", "Steel.Effect.fst.checked", "Steel.Effect.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.NMSTTotal.fst.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "Steel.Effect.Atomic.fst" }
[ { "abbrev": true, "full_module": "FStar.Universe", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "Steel.Semantics.Hoare.MST", "short_module": "Sem" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Common", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics", "short_module": "T" }, { "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": 1, "initial_ifuel": 1, "max_fuel": 1, "max_ifuel": 1, "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": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
v: Steel.Effect.Common.vprop -> q: Steel.Effect.Common.vprop -> p: (_: Steel.Effect.Common.t_of v -> Steel.Effect.Common.vprop) -> Steel.Effect.Atomic.SteelGhost Prims.unit
Steel.Effect.Atomic.SteelGhost
[]
[]
[ "Steel.Memory.inames", "Steel.Effect.Common.vprop", "Steel.Effect.Common.t_of", "Steel.Effect.Atomic.change_slprop_rel_with_cond", "Steel.Effect.Common.star", "Steel.Effect.Common.vdep", "Steel.Effect.Atomic.vdep_cond", "Steel.Effect.Atomic.vdep_rel", "Steel.Memory.mem", "Steel.Effect.Atomic.intro_vdep_lemma", "Prims.unit", "Steel.Effect.Atomic.reveal_star" ]
[]
false
true
false
false
false
let intro_vdep v q p =
reveal_star v q; change_slprop_rel_with_cond (v `star` q) (vdep v p) (vdep_cond v q p) (vdep_rel v q p) (fun m -> intro_vdep_lemma v q p m)
false
LowParseWriters.fsti
LowParseWriters.return_spec
val return_spec (a: Type) (x: a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False))
val return_spec (a: Type) (x: a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False))
let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 25, "end_line": 993, "start_col": 0, "start_line": 990 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> x: a -> r: LowParseWriters.LowParse.parser -> LowParseWriters.repr_spec a r r (fun _ -> Prims.l_True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> Prims.l_False)
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.Correct", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Mktuple2", "LowParseWriters.result", "LowParseWriters.repr_spec", "Prims.l_True", "Prims.l_and", "Prims.eq2", "Prims.l_False" ]
[]
false
false
false
false
false
let return_spec (a: Type) (x: a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) =
fun c -> Correct (x, c)
false
LowParseWriters.fsti
LowParseWriters.frame_pre
val frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame)
val frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame)
let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre ()
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 17, "end_line": 1434, "start_col": 0, "start_line": 1430 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t LowParseWriters.LowParse.parse_empty -> LowParseWriters.pre_t frame
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.LowParse.parse_empty", "LowParseWriters.LowParse.__proj__Parser__item__t" ]
[]
false
false
false
false
false
let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) =
fun _ -> pre ()
false
LowParseWriters.fsti
LowParseWriters.recast_writer_post
val recast_writer_post (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot (post_t a r_in r_out pre)
val recast_writer_post (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot (post_t a r_in r_out pre)
let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 132, "end_line": 1565, "start_col": 0, "start_line": 1554 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> r_in: LowParseWriters.LowParse.parser -> r_out: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t r_in -> post: LowParseWriters.post_t a r_in r_out pre -> post_err: LowParseWriters.post_err_t r_in pre -> l: LowParseWriters.memory_invariant -> f: (_: Prims.unit -> LowParseWriters.EWrite a) -> LowParseWriters.post_t a r_in r_out pre
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.memory_invariant", "Prims.unit", "LowParseWriters.LowParse.__proj__Parser__item__t", "Prims.l_and", "Prims.eq2", "LowParseWriters.result", "FStar.Pervasives.Native.tuple2", "LowParseWriters.destr_repr_spec", "LowParseWriters.Correct", "FStar.Pervasives.Native.Mktuple2" ]
[]
false
false
false
false
false
let recast_writer_post (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot (post_t a r_in r_out pre) =
fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out
false
LowParseWriters.fsti
LowParseWriters.recast_writer_post_err
val recast_writer_post_err (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot (post_err_t r_in pre)
val recast_writer_post_err (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot (post_err_t r_in pre)
let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 107, "end_line": 1579, "start_col": 0, "start_line": 1568 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> r_in: LowParseWriters.LowParse.parser -> r_out: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t r_in -> post: LowParseWriters.post_t a r_in r_out pre -> post_err: LowParseWriters.post_err_t r_in pre -> l: LowParseWriters.memory_invariant -> f: (_: Prims.unit -> LowParseWriters.EWrite a) -> LowParseWriters.post_err_t r_in pre
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.memory_invariant", "Prims.unit", "LowParseWriters.LowParse.__proj__Parser__item__t", "Prims.l_and", "Prims.b2t", "LowParseWriters.uu___is_Error", "FStar.Pervasives.Native.tuple2", "LowParseWriters.destr_repr_spec" ]
[]
false
false
false
false
false
let recast_writer_post_err (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot (post_err_t r_in pre) =
fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in
false
LowParseWriters.fsti
LowParseWriters.frame2_pre
val frame2_pre (frame ppre: parser) (pre: pre_t ppre) : Tot (pre_t (frame `parse_pair` ppre))
val frame2_pre (frame ppre: parser) (pre: pre_t ppre) : Tot (pre_t (frame `parse_pair` ppre))
let frame2_pre (frame: parser) (ppre: parser) (pre: pre_t ppre) : Tot (pre_t (frame `parse_pair` ppre)) = fun (_, x) -> pre x
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 21, "end_line": 1661, "start_col": 0, "start_line": 1656 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in inline_for_extraction val recast_writer_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f)) inline_for_extraction let recast_writer_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) = mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f) inline_for_extraction let recast_writer (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: unit -> EWrite a r_in r_out pre post post_err l) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l = recast_writer_repr a r_in r_out pre post post_err l f () inline_for_extraction let frame' (a: Type) (fr: parser) (p: parser) (l: memory_invariant) (f: unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l ) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout) ) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ()) ) l = frame _ _ _ _ _ _ _ (fun _ -> recast_writer _ _ _ _ _ _ _ f)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.LowParse.parser -> ppre: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t ppre -> LowParseWriters.pre_t (LowParseWriters.LowParse.parse_pair frame ppre)
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.LowParse.parse_pair" ]
[]
false
false
false
false
false
let frame2_pre (frame ppre: parser) (pre: pre_t ppre) : Tot (pre_t (frame `parse_pair` ppre)) =
fun (_, x) -> pre x
false
LowParseWriters.fsti
LowParseWriters.frame2_post
val frame2_post (a: Type) (frame ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) : Tot (post_t a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre))
val frame2_post (a: Type) (frame ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) : Tot (post_t a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre))
let frame2_post (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) : Tot (post_t a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre)) = fun (v_frame, v_in) v (v_frame', v_out) -> v_frame == v_frame' /\ post v_in v v_out
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 85, "end_line": 1672, "start_col": 0, "start_line": 1664 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in inline_for_extraction val recast_writer_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f)) inline_for_extraction let recast_writer_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) = mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f) inline_for_extraction let recast_writer (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: unit -> EWrite a r_in r_out pre post post_err l) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l = recast_writer_repr a r_in r_out pre post post_err l f () inline_for_extraction let frame' (a: Type) (fr: parser) (p: parser) (l: memory_invariant) (f: unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l ) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout) ) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ()) ) l = frame _ _ _ _ _ _ _ (fun _ -> recast_writer _ _ _ _ _ _ _ f) unfold let frame2_pre (frame: parser) (ppre: parser) (pre: pre_t ppre) : Tot (pre_t (frame `parse_pair` ppre)) = fun (_, x) -> pre x
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> frame: LowParseWriters.LowParse.parser -> ppre: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t ppre -> p: LowParseWriters.LowParse.parser -> post: LowParseWriters.post_t a ppre p pre -> LowParseWriters.post_t a (LowParseWriters.LowParse.parse_pair frame ppre) (LowParseWriters.frame_out a frame p) (LowParseWriters.frame2_pre frame ppre pre)
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.LowParse.parse_pair", "LowParseWriters.frame_out", "FStar.Pervasives.Native.Mktuple2", "FStar.Pervasives.Native.tuple2", "Prims.l_and", "Prims.eq2", "LowParseWriters.frame2_pre" ]
[]
false
false
false
false
false
let frame2_post (a: Type) (frame ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) : Tot (post_t a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre)) =
fun (v_frame, v_in) v (v_frame', v_out) -> v_frame == v_frame' /\ post v_in v v_out
false
LowParseWriters.fsti
LowParseWriters.frame2_post_err
val frame2_post_err (frame ppre: parser) (pre: pre_t ppre) (post_err: post_err_t ppre pre) : Tot (post_err_t (frame `parse_pair` ppre) (frame2_pre frame ppre pre))
val frame2_post_err (frame ppre: parser) (pre: pre_t ppre) (post_err: post_err_t ppre pre) : Tot (post_err_t (frame `parse_pair` ppre) (frame2_pre frame ppre pre))
let frame2_post_err (frame: parser) (ppre: parser) (pre: pre_t ppre) (post_err: post_err_t ppre pre) : Tot (post_err_t (frame `parse_pair` ppre) (frame2_pre frame ppre pre)) = fun (_, x) -> post_err x
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 26, "end_line": 1681, "start_col": 0, "start_line": 1675 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in inline_for_extraction val recast_writer_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f)) inline_for_extraction let recast_writer_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) = mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f) inline_for_extraction let recast_writer (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: unit -> EWrite a r_in r_out pre post post_err l) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l = recast_writer_repr a r_in r_out pre post post_err l f () inline_for_extraction let frame' (a: Type) (fr: parser) (p: parser) (l: memory_invariant) (f: unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l ) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout) ) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ()) ) l = frame _ _ _ _ _ _ _ (fun _ -> recast_writer _ _ _ _ _ _ _ f) unfold let frame2_pre (frame: parser) (ppre: parser) (pre: pre_t ppre) : Tot (pre_t (frame `parse_pair` ppre)) = fun (_, x) -> pre x unfold let frame2_post (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) : Tot (post_t a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre)) = fun (v_frame, v_in) v (v_frame', v_out) -> v_frame == v_frame' /\ post v_in v v_out
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.LowParse.parser -> ppre: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t ppre -> post_err: LowParseWriters.post_err_t ppre pre -> LowParseWriters.post_err_t (LowParseWriters.LowParse.parse_pair frame ppre) (LowParseWriters.frame2_pre frame ppre pre)
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_err_t", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.LowParse.parse_pair", "LowParseWriters.frame2_pre" ]
[]
false
false
false
false
false
let frame2_post_err (frame ppre: parser) (pre: pre_t ppre) (post_err: post_err_t ppre pre) : Tot (post_err_t (frame `parse_pair` ppre) (frame2_pre frame ppre pre)) =
fun (_, x) -> post_err x
false
LowParseWriters.fsti
LowParseWriters.lift_read_spec
val lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) (fun st x st' -> st == st' /\ post x) (fun _ -> post_err ()))
val lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) (fun st x st' -> st == st' /\ post x) (fun _ -> post_err ()))
let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 22, "end_line": 1231, "start_col": 0, "start_line": 1215 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> pre: Prims.pure_pre -> post: LowParseWriters.pure_post' a pre -> post_err: LowParseWriters.pure_post_err pre -> inv: LowParseWriters.memory_invariant -> r: LowParseWriters.LowParse.parser -> f_read_spec: LowParseWriters.read_repr a pre post post_err inv -> LowParseWriters.repr_spec a r r (fun _ -> pre) (fun st x st' -> st == st' /\ post x) (fun _ -> post_err ())
Prims.Tot
[ "total" ]
[]
[ "Prims.pure_pre", "LowParseWriters.pure_post'", "LowParseWriters.pure_post_err", "LowParseWriters.memory_invariant", "LowParseWriters.LowParse.parser", "LowParseWriters.read_repr", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.__proj__ReadRepr__item__spec", "LowParseWriters.Correct", "FStar.Pervasives.Native.tuple2", "FStar.Pervasives.Native.Mktuple2", "Prims.string", "LowParseWriters.Error", "LowParseWriters.result", "LowParseWriters.repr_spec", "Prims.l_and", "Prims.eq2" ]
[]
false
false
false
false
false
let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) (fun st x st' -> st == st' /\ post x) (fun _ -> post_err ())) =
fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e
false
Pulse.C.Types.UserStruct.fsti
Pulse.C.Types.UserStruct.set_def
val set_def (#t: eqtype) (s: FStar.Set.set t) (x: t) : Tot bool
val set_def (#t: eqtype) (s: FStar.Set.set t) (x: t) : Tot bool
let set_def (#t: eqtype) (s: FStar.Set.set t) (x: t) : Tot bool = FStar.Set.mem x s
{ "file_name": "share/steel/examples/pulse/lib/c/Pulse.C.Types.UserStruct.fsti", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 19, "end_line": 16, "start_col": 0, "start_line": 11 }
module Pulse.C.Types.UserStruct open Pulse.Lib.Pervasives open Pulse.C.Types.Struct.Aux module Set = FStar.Set (* This library allows the user to define their own struct type, with a constructor from field values, and a destructor to field values for each field. This may be necessary for recursive structs *)
{ "checked_file": "/", "dependencies": [ "Pulse.Lib.Pervasives.fst.checked", "Pulse.C.Types.Struct.Aux.fsti.checked", "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Pulse.C.Types.UserStruct.fsti" }
[ { "abbrev": true, "full_module": "FStar.Set", "short_module": "Set" }, { "abbrev": false, "full_module": "Pulse.C.Types.Struct.Aux", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Lib.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Pulse.C.Types", "short_module": null }, { "abbrev": false, "full_module": "Pulse.C.Types", "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
s: FStar.Set.set t -> x: t -> Prims.bool
Prims.Tot
[ "total" ]
[]
[ "Prims.eqtype", "FStar.Set.set", "FStar.Set.mem", "Prims.bool" ]
[]
false
false
false
false
false
let set_def (#t: eqtype) (s: FStar.Set.set t) (x: t) : Tot bool =
FStar.Set.mem x s
false
LowParseWriters.fsti
LowParseWriters.bind_spec
val bind_spec (a b: Type) (r_in_f r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x: a -> pre_t (r_out_f))) (post_g: (x: a -> post_t b (r_out_f) r_out_g (pre_g x))) (post_err_g: (x: a -> post_err_t (r_out_f) (pre_g x))) (f_bind_spec: repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g: (x: a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out. post_f v_in x v_out ==> pre_g x v_out)) (fun v_in y v_out -> exists x v_out_f. pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) (fun v_in -> pre_f v_in /\ (post_err_f v_in \/ (exists x v_out_f. post_f v_in x v_out_f /\ post_err_g x v_out_f))))
val bind_spec (a b: Type) (r_in_f r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x: a -> pre_t (r_out_f))) (post_g: (x: a -> post_t b (r_out_f) r_out_g (pre_g x))) (post_err_g: (x: a -> post_err_t (r_out_f) (pre_g x))) (f_bind_spec: repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g: (x: a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out. post_f v_in x v_out ==> pre_g x v_out)) (fun v_in y v_out -> exists x v_out_f. pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) (fun v_in -> pre_f v_in /\ (post_err_f v_in \/ (exists x v_out_f. post_f v_in x v_out_f /\ post_err_g x v_out_f))))
let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 22, "end_line": 1029, "start_col": 0, "start_line": 1007 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> b: Type -> r_in_f: LowParseWriters.LowParse.parser -> r_out_f: LowParseWriters.LowParse.parser -> pre_f: LowParseWriters.pre_t r_in_f -> post_f: LowParseWriters.post_t a r_in_f r_out_f pre_f -> post_err_f: LowParseWriters.post_err_t r_in_f pre_f -> r_out_g: LowParseWriters.LowParse.parser -> pre_g: (x: a -> LowParseWriters.pre_t r_out_f) -> post_g: (x: a -> LowParseWriters.post_t b r_out_f r_out_g (pre_g x)) -> post_err_g: (x: a -> LowParseWriters.post_err_t r_out_f (pre_g x)) -> f_bind_spec: LowParseWriters.repr_spec a r_in_f r_out_f pre_f post_f post_err_f -> g: (x: a -> LowParseWriters.repr_spec b r_out_f r_out_g (pre_g x) (post_g x) (post_err_g x)) -> LowParseWriters.repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) (v_out: Parser?.t r_out_f). post_f v_in x v_out ==> pre_g x v_out)) (fun v_in y v_out -> exists (x: a) (v_out_f: Parser?.t r_out_f). pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) (fun v_in -> pre_f v_in /\ (post_err_f v_in \/ (exists (x: a) (v_out_f: Parser?.t r_out_f). post_f v_in x v_out_f /\ post_err_g x v_out_f)))
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.repr_spec", "LowParseWriters.LowParse.__proj__Parser__item__t", "Prims.string", "LowParseWriters.Error", "FStar.Pervasives.Native.tuple2", "LowParseWriters.result", "Prims.l_and", "Prims.l_Forall", "Prims.l_imp", "Prims.l_Exists", "Prims.l_or" ]
[]
false
false
false
false
false
let bind_spec (a b: Type) (r_in_f r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x: a -> pre_t (r_out_f))) (post_g: (x: a -> post_t b (r_out_f) r_out_g (pre_g x))) (post_err_g: (x: a -> post_err_t (r_out_f) (pre_g x))) (f_bind_spec: repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g: (x: a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out. post_f v_in x v_out ==> pre_g x v_out)) (fun v_in y v_out -> exists x v_out_f. pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) (fun v_in -> pre_f v_in /\ (post_err_f v_in \/ (exists x v_out_f. post_f v_in x v_out_f /\ post_err_g x v_out_f)))) =
fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e
false
LowParseWriters.fsti
LowParseWriters.wrap_extracted_impl
val wrap_extracted_impl (#a: Type u#x) (#r_in #r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: (unit -> EWrite a r_in r_out pre post post_err l)) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l
val wrap_extracted_impl (#a: Type u#x) (#r_in #r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: (unit -> EWrite a r_in r_out pre post post_err l)) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l
let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) ))
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 4, "end_line": 1385, "start_col": 0, "start_line": 1371 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.memory_invariant -> $f_destr_spec: (_: Prims.unit -> LowParseWriters.EWrite a) -> e: LowParseWriters.extract_t l f_destr_spec -> LowParseWriters.EWrite a
LowParseWriters.EWrite
[]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.memory_invariant", "Prims.unit", "LowParseWriters.extract_t", "LowParseWriters.Repr", "LowParseWriters.destr_repr_spec", "LowParseWriters.mk_repr_impl", "LowStar.Buffer.buffer", "LowParseWriters.LowParse.u8", "LowStar.Monotonic.Buffer.loc_includes", "FStar.Ghost.reveal", "LowStar.Monotonic.Buffer.loc", "LowParseWriters.__proj__Mkmemory_invariant__item__lwrite", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Buffer.trivial_preorder", "FStar.UInt32.t", "Prims.eq2", "LowStar.Monotonic.Buffer.len", "LowParseWriters.buffer_offset", "LowParseWriters.iresult" ]
[]
false
true
false
false
false
let wrap_extracted_impl (#a: Type u#x) (#r_in #r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: (unit -> EWrite a r_in r_out pre post post_err l)) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l =
EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1)))
false
Pulse.C.Types.UserStruct.fsti
Pulse.C.Types.UserStruct.nonempty_set
val nonempty_set : t: Prims.eqtype -> Type0
let nonempty_set (t: eqtype) = (s: Set.set t { exists x . set_def s x == true })
{ "file_name": "share/steel/examples/pulse/lib/c/Pulse.C.Types.UserStruct.fsti", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 51, "end_line": 20, "start_col": 0, "start_line": 19 }
module Pulse.C.Types.UserStruct open Pulse.Lib.Pervasives open Pulse.C.Types.Struct.Aux module Set = FStar.Set (* This library allows the user to define their own struct type, with a constructor from field values, and a destructor to field values for each field. This may be necessary for recursive structs *) let set_def (#t: eqtype) (s: FStar.Set.set t) (x: t) : Tot bool = FStar.Set.mem x s
{ "checked_file": "/", "dependencies": [ "Pulse.Lib.Pervasives.fst.checked", "Pulse.C.Types.Struct.Aux.fsti.checked", "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Pulse.C.Types.UserStruct.fsti" }
[ { "abbrev": true, "full_module": "FStar.Set", "short_module": "Set" }, { "abbrev": false, "full_module": "Pulse.C.Types.Struct.Aux", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Lib.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Pulse.C.Types", "short_module": null }, { "abbrev": false, "full_module": "Pulse.C.Types", "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: Prims.eqtype -> Type0
Prims.Tot
[ "total" ]
[]
[ "Prims.eqtype", "FStar.Set.set", "Prims.l_Exists", "Prims.eq2", "Prims.bool", "Pulse.C.Types.UserStruct.set_def" ]
[]
false
false
false
true
true
let nonempty_set (t: eqtype) =
(s: Set.set t {exists x. set_def s x == true})
false
LowParseWriters.fsti
LowParseWriters.subcomp
val subcomp (a: Type) ([@@@ refl_implicit]r_in [@@@ refl_implicit]r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit]l: memory_invariant) ([@@@ refl_implicit]r_in' [@@@ refl_implicit]r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit]l': memory_invariant) ([@@@ refl_implicit]pr1: squash (r_in == r_in')) ([@@@ refl_implicit]pr2: squash (r_out == r_out')) (f_subcomp: repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True))
val subcomp (a: Type) ([@@@ refl_implicit]r_in [@@@ refl_implicit]r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit]l: memory_invariant) ([@@@ refl_implicit]r_in' [@@@ refl_implicit]r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit]l': memory_invariant) ([@@@ refl_implicit]pr1: squash (r_in == r_in')) ([@@@ refl_implicit]pr2: squash (r_out == r_out')) (f_subcomp: repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True))
let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () )
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 1138, "start_col": 0, "start_line": 1121 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> r_in: LowParseWriters.LowParse.parser -> r_out: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t r_in -> post: LowParseWriters.post_t a r_in r_out pre -> post_err: LowParseWriters.post_err_t r_in pre -> l: LowParseWriters.memory_invariant -> r_in': LowParseWriters.LowParse.parser -> r_out': LowParseWriters.LowParse.parser -> pre': LowParseWriters.pre_t r_in' -> post': LowParseWriters.post_t a r_in' r_out' pre' -> post_err': LowParseWriters.post_err_t r_in' pre' -> l': LowParseWriters.memory_invariant -> pr1: Prims.squash (r_in == r_in') -> pr2: Prims.squash (r_out == r_out') -> f_subcomp: LowParseWriters.repr a r_in r_out pre post post_err l -> Prims.Pure (LowParseWriters.repr a r_in' r_out' pre' post' post_err' l')
Prims.Pure
[]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.memory_invariant", "Prims.squash", "Prims.eq2", "LowParseWriters.repr", "LowParseWriters.Repr", "LowParseWriters.subcomp_spec", "LowParseWriters.__proj__Repr__item__spec", "LowParseWriters.subcomp_impl", "LowParseWriters.__proj__Repr__item__impl", "LowParseWriters.subcomp_cond", "Prims.l_True" ]
[]
false
false
false
false
false
let subcomp (a: Type) ([@@@ refl_implicit]r_in [@@@ refl_implicit]r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit]l: memory_invariant) ([@@@ refl_implicit]r_in' [@@@ refl_implicit]r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit]l': memory_invariant) ([@@@ refl_implicit]pr1: squash (r_in == r_in')) ([@@@ refl_implicit]pr2: squash (r_out == r_out')) (f_subcomp: repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) =
Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) ())
false
Pulse.C.Types.UserStruct.fsti
Pulse.C.Types.UserStruct.set
val set (#t: Type) (sd: struct_def t) (x: t) (f: field_t sd.fields) (v: sd.field_desc.fd_type f) : Tot t
val set (#t: Type) (sd: struct_def t) (x: t) (f: field_t sd.fields) (v: sd.field_desc.fd_type f) : Tot t
let set (#t: Type) (sd: struct_def t) (x: t) (f: field_t sd.fields) (v: sd.field_desc.fd_type f) : Tot t = sd.mk (set_aux sd x f v)
{ "file_name": "share/steel/examples/pulse/lib/c/Pulse.C.Types.UserStruct.fsti", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 26, "end_line": 61, "start_col": 0, "start_line": 60 }
module Pulse.C.Types.UserStruct open Pulse.Lib.Pervasives open Pulse.C.Types.Struct.Aux module Set = FStar.Set (* This library allows the user to define their own struct type, with a constructor from field values, and a destructor to field values for each field. This may be necessary for recursive structs *) let set_def (#t: eqtype) (s: FStar.Set.set t) (x: t) : Tot bool = FStar.Set.mem x s noextract let nonempty_set (t: eqtype) = (s: Set.set t { exists x . set_def s x == true }) noextract let set_snoc // for associativity reasons (#t: eqtype) (q: FStar.Set.set t) (a: t) : Pure (nonempty_set t) (requires True) (ensures (fun s -> (forall (x: t). {:pattern FStar.Set.mem x s} FStar.Set.mem x s == (x = a || FStar.Set.mem x q)) )) = q `FStar.Set.union` FStar.Set.singleton a [@@noextract_to "krml"] let field_t (s: Set.set string) : Tot eqtype = (f: string { Set.mem f s }) [@@noextract_to "krml"; norm_field_attr] inline_for_extraction // for field_desc.fd_type noeq type struct_def (t: Type) = { fields: Set.set string; field_desc: field_description_gen_t (field_t fields); mk: ((f: field_t fields) -> Tot (field_desc.fd_type f)) -> Tot t; get: (t -> (f: field_t fields) -> Tot (field_desc.fd_type f)); get_mk: (phi: ((f: field_t fields) -> Tot (field_desc.fd_type f))) -> (f: field_t fields) -> Lemma (get (mk phi) f == phi f); extensionality: (x1: t) -> (x2: t) -> ((f: field_t fields) -> Lemma (get x1 f == get x2 f)) -> Lemma (x1 == x2); } let nonempty_set_nonempty_type (x: string) (s: Set.set string) : Lemma (requires (x `Set.mem` s)) (ensures (exists (x: field_t s) . True)) = Classical.exists_intro (fun (_: field_t s) -> True) x [@@noextract_to "krml"] let set_aux (#t: Type) (sd: struct_def t) (x: t) (f: field_t sd.fields) (v: sd.field_desc.fd_type f) (f': field_t sd.fields) : Tot (sd.field_desc.fd_type f') = if f = f' then v else sd.get x f'
{ "checked_file": "/", "dependencies": [ "Pulse.Lib.Pervasives.fst.checked", "Pulse.C.Types.Struct.Aux.fsti.checked", "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Pulse.C.Types.UserStruct.fsti" }
[ { "abbrev": true, "full_module": "FStar.Set", "short_module": "Set" }, { "abbrev": false, "full_module": "Pulse.C.Types.Struct.Aux", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Lib.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Pulse.C.Types", "short_module": null }, { "abbrev": false, "full_module": "Pulse.C.Types", "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
sd: Pulse.C.Types.UserStruct.struct_def t -> x: t -> f: Pulse.C.Types.UserStruct.field_t (Mkstruct_def?.fields sd) -> v: Mkfield_description_gen_t?.fd_type (Mkstruct_def?.field_desc sd) f -> t
Prims.Tot
[ "total" ]
[]
[ "Pulse.C.Types.UserStruct.struct_def", "Pulse.C.Types.UserStruct.field_t", "Pulse.C.Types.UserStruct.__proj__Mkstruct_def__item__fields", "Pulse.C.Types.Struct.Aux.__proj__Mkfield_description_gen_t__item__fd_type", "Pulse.C.Types.UserStruct.__proj__Mkstruct_def__item__field_desc", "Pulse.C.Types.UserStruct.__proj__Mkstruct_def__item__mk", "Pulse.C.Types.UserStruct.set_aux" ]
[]
false
false
false
false
false
let set (#t: Type) (sd: struct_def t) (x: t) (f: field_t sd.fields) (v: sd.field_desc.fd_type f) : Tot t =
sd.mk (set_aux sd x f v)
false
LowParseWriters.fsti
LowParseWriters.subcomp_spec
val subcomp_spec (a: Type) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp: repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True))
val subcomp_spec (a: Type) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp: repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True))
let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 24, "end_line": 1105, "start_col": 0, "start_line": 1097 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err'
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> r_in: LowParseWriters.LowParse.parser -> r_out: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t r_in -> post: LowParseWriters.post_t a r_in r_out pre -> post_err: LowParseWriters.post_err_t r_in pre -> pre': LowParseWriters.pre_t r_in -> post': LowParseWriters.post_t a r_in r_out pre' -> post_err': LowParseWriters.post_err_t r_in pre' -> f_subcomp: LowParseWriters.repr_spec a r_in r_out pre post post_err -> Prims.Pure (LowParseWriters.repr_spec a r_in r_out pre' post' post_err')
Prims.Pure
[]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.repr_spec", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.result", "FStar.Pervasives.Native.tuple2", "LowParseWriters.subcomp_spec_cond", "Prims.l_True" ]
[]
false
false
false
false
false
let subcomp_spec (a: Type) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp: repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) =
(fun x -> f_subcomp x)
false
LowParseWriters.fsti
LowParseWriters.wfailwith_spec
val wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True))
val wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True))
let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 18, "end_line": 1268, "start_col": 0, "start_line": 1263 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> rin: LowParseWriters.LowParse.parser -> rout: LowParseWriters.LowParse.parser -> s: Prims.string -> LowParseWriters.repr_spec a rin rout (fun _ -> Prims.l_True) (fun _ _ _ -> Prims.l_False) (fun _ -> Prims.l_True)
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "Prims.string", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.Error", "FStar.Pervasives.Native.tuple2", "LowParseWriters.result", "LowParseWriters.repr_spec", "Prims.l_True", "Prims.l_False" ]
[]
false
false
false
false
false
let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) =
fun _ -> Error s
false
Pulse.C.Types.UserStruct.fsti
Pulse.C.Types.UserStruct.field_t
val field_t (s: Set.set string) : Tot eqtype
val field_t (s: Set.set string) : Tot eqtype
let field_t (s: Set.set string) : Tot eqtype = (f: string { Set.mem f s })
{ "file_name": "share/steel/examples/pulse/lib/c/Pulse.C.Types.UserStruct.fsti", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 29, "end_line": 33, "start_col": 0, "start_line": 32 }
module Pulse.C.Types.UserStruct open Pulse.Lib.Pervasives open Pulse.C.Types.Struct.Aux module Set = FStar.Set (* This library allows the user to define their own struct type, with a constructor from field values, and a destructor to field values for each field. This may be necessary for recursive structs *) let set_def (#t: eqtype) (s: FStar.Set.set t) (x: t) : Tot bool = FStar.Set.mem x s noextract let nonempty_set (t: eqtype) = (s: Set.set t { exists x . set_def s x == true }) noextract let set_snoc // for associativity reasons (#t: eqtype) (q: FStar.Set.set t) (a: t) : Pure (nonempty_set t) (requires True) (ensures (fun s -> (forall (x: t). {:pattern FStar.Set.mem x s} FStar.Set.mem x s == (x = a || FStar.Set.mem x q)) )) = q `FStar.Set.union` FStar.Set.singleton a
{ "checked_file": "/", "dependencies": [ "Pulse.Lib.Pervasives.fst.checked", "Pulse.C.Types.Struct.Aux.fsti.checked", "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Pulse.C.Types.UserStruct.fsti" }
[ { "abbrev": true, "full_module": "FStar.Set", "short_module": "Set" }, { "abbrev": false, "full_module": "Pulse.C.Types.Struct.Aux", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Lib.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Pulse.C.Types", "short_module": null }, { "abbrev": false, "full_module": "Pulse.C.Types", "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
s: FStar.Set.set Prims.string -> Prims.eqtype
Prims.Tot
[ "total" ]
[]
[ "FStar.Set.set", "Prims.string", "Prims.b2t", "FStar.Set.mem", "Prims.eqtype" ]
[]
false
false
false
true
false
let field_t (s: Set.set string) : Tot eqtype =
(f: string{Set.mem f s})
false
LowParseWriters.fsti
LowParseWriters.bind
val bind (a b: Type) (r_in_f [@@@ refl_implicit]r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit]l_f: memory_invariant) ([@@@ refl_implicit]r_in_g r_out_g: parser) (pre_g: (x: a -> pre_t r_in_g)) (post_g: (x: a -> post_t b r_in_g r_out_g (pre_g x))) (post_err_g: (x: a -> post_err_t r_in_g (pre_g x))) ([@@@ refl_implicit]l_g: memory_invariant) ([@@@ refl_implicit]pr: squash (l_f == l_g)) ([@@@ refl_implicit]pr': squash (r_out_f == r_in_g)) (f_bind: repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g: (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out. post_f v_in x v_out ==> pre_g x v_out)) (fun v_in y v_out -> exists x v_out_f. pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) (fun v_in -> pre_f v_in /\ (post_err_f v_in \/ (exists x v_out_f. post_f v_in x v_out_f /\ post_err_g x v_out_f))) l_g)
val bind (a b: Type) (r_in_f [@@@ refl_implicit]r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit]l_f: memory_invariant) ([@@@ refl_implicit]r_in_g r_out_g: parser) (pre_g: (x: a -> pre_t r_in_g)) (post_g: (x: a -> post_t b r_in_g r_out_g (pre_g x))) (post_err_g: (x: a -> post_err_t r_in_g (pre_g x))) ([@@@ refl_implicit]l_g: memory_invariant) ([@@@ refl_implicit]pr: squash (l_f == l_g)) ([@@@ refl_implicit]pr': squash (r_out_f == r_in_g)) (f_bind: repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g: (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out. post_f v_in x v_out ==> pre_g x v_out)) (fun v_in y v_out -> exists x v_out_f. pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) (fun v_in -> pre_f v_in /\ (post_err_f v_in \/ (exists x v_out_f. post_f v_in x v_out_f /\ post_err_g x v_out_f))) l_g)
let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x)))
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 328, "end_line": 1072, "start_col": 0, "start_line": 1048 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> b: Type -> r_in_f: LowParseWriters.LowParse.parser -> r_out_f: LowParseWriters.LowParse.parser -> pre_f: LowParseWriters.pre_t r_in_f -> post_f: LowParseWriters.post_t a r_in_f r_out_f pre_f -> post_err_f: LowParseWriters.post_err_t r_in_f pre_f -> l_f: LowParseWriters.memory_invariant -> r_in_g: LowParseWriters.LowParse.parser -> r_out_g: LowParseWriters.LowParse.parser -> pre_g: (x: a -> LowParseWriters.pre_t r_in_g) -> post_g: (x: a -> LowParseWriters.post_t b r_in_g r_out_g (pre_g x)) -> post_err_g: (x: a -> LowParseWriters.post_err_t r_in_g (pre_g x)) -> l_g: LowParseWriters.memory_invariant -> pr: Prims.squash (l_f == l_g) -> pr': Prims.squash (r_out_f == r_in_g) -> f_bind: LowParseWriters.repr a r_in_f r_out_f pre_f post_f post_err_f l_f -> g: (x: a -> LowParseWriters.repr b r_in_g r_out_g (pre_g x) (post_g x) (post_err_g x) l_g) -> LowParseWriters.repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) (v_out: Parser?.t r_out_f). post_f v_in x v_out ==> pre_g x v_out)) (fun v_in y v_out -> exists (x: a) (v_out_f: Parser?.t r_in_g). pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) (fun v_in -> pre_f v_in /\ (post_err_f v_in \/ (exists (x: a) (v_out_f: Parser?.t r_in_g). post_f v_in x v_out_f /\ post_err_g x v_out_f))) l_g
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.memory_invariant", "Prims.squash", "Prims.eq2", "LowParseWriters.repr", "LowParseWriters.Repr", "LowParseWriters.LowParse.__proj__Parser__item__t", "Prims.l_and", "Prims.l_Forall", "Prims.l_imp", "Prims.l_Exists", "Prims.l_or", "LowParseWriters.bind_spec", "LowParseWriters.__proj__Repr__item__spec", "LowParseWriters.repr_spec", "LowParseWriters.bind_impl", "LowParseWriters.__proj__Repr__item__impl", "LowParseWriters.repr_impl" ]
[]
false
false
false
false
false
let bind (a b: Type) (r_in_f [@@@ refl_implicit]r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit]l_f: memory_invariant) ([@@@ refl_implicit]r_in_g r_out_g: parser) (pre_g: (x: a -> pre_t r_in_g)) (post_g: (x: a -> post_t b r_in_g r_out_g (pre_g x))) (post_err_g: (x: a -> post_err_t r_in_g (pre_g x))) ([@@@ refl_implicit]l_g: memory_invariant) ([@@@ refl_implicit]pr: squash (l_f == l_g)) ([@@@ refl_implicit]pr': squash (r_out_f == r_in_g)) (f_bind: repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g: (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out. post_f v_in x v_out ==> pre_g x v_out)) (fun v_in y v_out -> exists x v_out_f. pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) (fun v_in -> pre_f v_in /\ (post_err_f v_in \/ (exists x v_out_f. post_f v_in x v_out_f /\ post_err_g x v_out_f))) l_g) =
Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x)))
false
LowParseWriters.fsti
LowParseWriters.extract_t
val extract_t (#a: Type u#x) (#r_in #r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot Type
val extract_t (#a: Type u#x) (#r_in #r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot Type
let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end )))
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 5, "end_line": 1355, "start_col": 0, "start_line": 1314 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ()))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.memory_invariant -> $f_destr_spec: (_: Prims.unit -> LowParseWriters.EWrite a) -> Type0
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.memory_invariant", "Prims.unit", "LowStar.Buffer.buffer", "LowParseWriters.LowParse.u8", "LowStar.Monotonic.Buffer.loc_includes", "FStar.Ghost.reveal", "LowStar.Monotonic.Buffer.loc", "LowParseWriters.__proj__Mkmemory_invariant__item__lwrite", "LowStar.Monotonic.Buffer.loc_buffer", "LowStar.Buffer.trivial_preorder", "FStar.UInt32.t", "Prims.eq2", "LowStar.Monotonic.Buffer.len", "LowParseWriters.buffer_offset", "LowParseWriters.iresult", "FStar.Monotonic.HyperStack.mem", "Prims.l_and", "LowStar.Monotonic.Buffer.modifies", "LowParseWriters.__proj__Mkmemory_invariant__item__h0", "Prims.b2t", "FStar.Monotonic.HyperHeap.includes", "FStar.Monotonic.HyperStack.get_tip", "LowParseWriters.LowParse.valid_pos", "FStar.UInt32.__uint_to_t", "LowParseWriters.LowParse.contents", "FStar.Pervasives.Native.Mktuple2", "LowParseWriters.result", "FStar.Pervasives.Native.tuple2", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.destr_repr_spec", "Prims.op_LessThanOrEqual", "FStar.UInt32.v", "Prims.op_GreaterThan", "LowParseWriters.LowParse.size", "LowStar.Monotonic.Buffer.length", "Prims.string", "Prims.l_True", "Prims.l_False", "Prims.logical" ]
[]
false
false
false
false
true
let extract_t (#a: Type u#x) (#r_in #r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot Type =
b: B.buffer u8 {l.lwrite `B.loc_includes` (B.loc_buffer b)} -> len: U32.t{len == B.len b} -> pos1: buffer_offset b -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ (HS.get_tip l.h0) `HS.includes` (HS.get_tip h) /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1))) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ (let v_in = contents r_in h b 0ul pos1 in pre v_in /\ (match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> True | _ -> False))))
false
LowParseWriters.fsti
LowParseWriters.get_state_spec
val get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False))
val get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False))
let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 36, "end_line": 1406, "start_col": 0, "start_line": 1403 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.LowParse.parser -> LowParseWriters.repr_spec (FStar.Ghost.erased (Parser?.t p)) p p (fun _ -> Prims.l_True) (fun x y z -> x == FStar.Ghost.reveal y /\ x == z) (fun _ -> Prims.l_False)
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.Correct", "FStar.Pervasives.Native.tuple2", "FStar.Ghost.erased", "FStar.Pervasives.Native.Mktuple2", "FStar.Ghost.hide", "LowParseWriters.result", "LowParseWriters.repr_spec", "Prims.l_True", "Prims.l_and", "Prims.eq2", "FStar.Ghost.reveal", "Prims.l_False" ]
[]
false
false
false
false
false
let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) =
fun x -> Correct (Ghost.hide x, x)
false
LowParseWriters.fsti
LowParseWriters.wfailwith
val wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv
val wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv
let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s))
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 60, "end_line": 1285, "start_col": 0, "start_line": 1279 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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
s: Prims.string -> LowParseWriters.EWrite a
LowParseWriters.EWrite
[]
[]
[ "LowParseWriters.memory_invariant", "LowParseWriters.LowParse.parser", "Prims.string", "LowParseWriters.Repr", "LowParseWriters.LowParse.__proj__Parser__item__t", "Prims.l_True", "Prims.l_False", "LowParseWriters.wfailwith_spec", "LowParseWriters.wfailwith_impl" ]
[]
false
true
false
false
false
let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv =
EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s))
false
LowParseWriters.fsti
LowParseWriters.elem_writer_spec
val elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False))
val elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False))
let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 26, "end_line": 1514, "start_col": 0, "start_line": 1510 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.LowParse.parser -> x: Parser?.t p -> LowParseWriters.repr_spec Prims.unit LowParseWriters.LowParse.parse_empty p (fun _ -> Prims.l_True) (fun _ _ y -> y == x) (fun _ -> Prims.l_False)
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.LowParse.parse_empty", "LowParseWriters.Correct", "FStar.Pervasives.Native.tuple2", "Prims.unit", "FStar.Pervasives.Native.Mktuple2", "LowParseWriters.result", "LowParseWriters.repr_spec", "Prims.l_True", "Prims.eq2", "Prims.l_False" ]
[]
false
false
false
false
false
let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) =
fun _ -> Correct ((), x)
false
LowParseWriters.fsti
LowParseWriters.mk_repr
val mk_repr: a: Type u#x -> r_in: parser -> r_out: parser -> pre: pre_t r_in -> post: post_t a r_in r_out pre -> post_err: post_err_t r_in pre -> l: memory_invariant -> spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> Prims.unit -> EWrite a r_in r_out pre post post_err l
val mk_repr: a: Type u#x -> r_in: parser -> r_out: parser -> pre: pre_t r_in -> post: post_t a r_in r_out pre -> post_err: post_err_t r_in pre -> l: memory_invariant -> spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> Prims.unit -> EWrite a r_in r_out pre post post_err l
let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 34, "end_line": 1401, "start_col": 0, "start_line": 1389 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> r_in: LowParseWriters.LowParse.parser -> r_out: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t r_in -> post: LowParseWriters.post_t a r_in r_out pre -> post_err: LowParseWriters.post_err_t r_in pre -> l: LowParseWriters.memory_invariant -> spec: LowParseWriters.repr_spec a r_in r_out pre post post_err -> impl: LowParseWriters.repr_impl a r_in r_out pre post post_err l spec -> _: Prims.unit -> LowParseWriters.EWrite a
LowParseWriters.EWrite
[]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.memory_invariant", "LowParseWriters.repr_spec", "LowParseWriters.repr_impl", "Prims.unit", "LowParseWriters.Repr" ]
[]
false
true
false
false
false
let mk_repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l =
EWrite?.reflect (Repr spec impl)
false
Hacl.P256.PrecompTable.fst
Hacl.P256.PrecompTable.precomp_basepoint_table_lseq_w4
val precomp_basepoint_table_lseq_w4 : LSeq.lseq uint64 192
val precomp_basepoint_table_lseq_w4 : LSeq.lseq uint64 192
let precomp_basepoint_table_lseq_w4 : LSeq.lseq uint64 192 = normalize_term_spec (SPT.precomp_base_table_list mk_p256_precomp_base_table S.base_point 15); Seq.seq_of_list precomp_basepoint_table_list_w4
{ "file_name": "code/ecdsap256/Hacl.P256.PrecompTable.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 49, "end_line": 205, "start_col": 0, "start_line": 203 }
module Hacl.P256.PrecompTable open FStar.HyperStack open FStar.HyperStack.ST open FStar.Mul open Lib.IntTypes open Lib.Buffer module ST = FStar.HyperStack.ST module LSeq = Lib.Sequence module LE = Lib.Exponentiation module SE = Spec.Exponentiation module SPT = Hacl.Spec.PrecompBaseTable module SPT256 = Hacl.Spec.PrecompBaseTable256 module SPTK = Hacl.Spec.P256.PrecompTable module S = Spec.P256 module SL = Spec.P256.Lemmas open Hacl.Impl.P256.Point include Hacl.Impl.P256.Group #set-options "--z3rlimit 50 --fuel 0 --ifuel 0" let proj_point_to_list p = SPTK.proj_point_to_list_lemma p; SPTK.proj_point_to_list p let lemma_refl x = SPTK.proj_point_to_list_lemma x //----------------- inline_for_extraction noextract let proj_g_pow2_64 : S.proj_point = [@inline_let] let rX : S.felem = 0x000931f4ae428a4ad81ee0aa89cf5247ce85d4dd696c61b4bb9d4761e57b7fbe in [@inline_let] let rY : S.felem = 0x7e88e5e6a142d5c2269f21a158e82ab2c79fcecb26e397b96fd5b9fbcd0a69a5 in [@inline_let] let rZ : S.felem = 0x02626dc2dd5e06cd19de5e6afb6c5dbdd3e41dc1472e7b8ef11eb0662e41c44b in (rX, rY, rZ) val lemma_proj_g_pow2_64_eval : unit -> Lemma (SE.exp_pow2 S.mk_p256_concrete_ops S.base_point 64 == proj_g_pow2_64) let lemma_proj_g_pow2_64_eval () = SPT256.exp_pow2_rec_is_exp_pow2 S.mk_p256_concrete_ops S.base_point 64; let qX, qY, qZ = normalize_term (SPT256.exp_pow2_rec S.mk_p256_concrete_ops S.base_point 64) in normalize_term_spec (SPT256.exp_pow2_rec S.mk_p256_concrete_ops S.base_point 64); let rX : S.felem = 0x000931f4ae428a4ad81ee0aa89cf5247ce85d4dd696c61b4bb9d4761e57b7fbe in let rY : S.felem = 0x7e88e5e6a142d5c2269f21a158e82ab2c79fcecb26e397b96fd5b9fbcd0a69a5 in let rZ : S.felem = 0x02626dc2dd5e06cd19de5e6afb6c5dbdd3e41dc1472e7b8ef11eb0662e41c44b in assert_norm (qX == rX /\ qY == rY /\ qZ == rZ) inline_for_extraction noextract let proj_g_pow2_128 : S.proj_point = [@inline_let] let rX : S.felem = 0x04c3aaf6c6c00704e96eda89461d63fd2c97ee1e6786fc785e6afac7aa92f9b1 in [@inline_let] let rY : S.felem = 0x14f1edaeb8e9c8d4797d164a3946c7ff50a7c8cd59139a4dbce354e6e4df09c3 in [@inline_let] let rZ : S.felem = 0x80119ced9a5ce83c4e31f8de1a38f89d5f9ff9f637dca86d116a4217f83e55d2 in (rX, rY, rZ) val lemma_proj_g_pow2_128_eval : unit -> Lemma (SE.exp_pow2 S.mk_p256_concrete_ops proj_g_pow2_64 64 == proj_g_pow2_128) let lemma_proj_g_pow2_128_eval () = SPT256.exp_pow2_rec_is_exp_pow2 S.mk_p256_concrete_ops proj_g_pow2_64 64; let qX, qY, qZ = normalize_term (SPT256.exp_pow2_rec S.mk_p256_concrete_ops proj_g_pow2_64 64) in normalize_term_spec (SPT256.exp_pow2_rec S.mk_p256_concrete_ops proj_g_pow2_64 64); let rX : S.felem = 0x04c3aaf6c6c00704e96eda89461d63fd2c97ee1e6786fc785e6afac7aa92f9b1 in let rY : S.felem = 0x14f1edaeb8e9c8d4797d164a3946c7ff50a7c8cd59139a4dbce354e6e4df09c3 in let rZ : S.felem = 0x80119ced9a5ce83c4e31f8de1a38f89d5f9ff9f637dca86d116a4217f83e55d2 in assert_norm (qX == rX /\ qY == rY /\ qZ == rZ) inline_for_extraction noextract let proj_g_pow2_192 : S.proj_point = [@inline_let] let rX : S.felem = 0xc762a9c8ae1b2f7434ff8da70fe105e0d4f188594989f193de0dbdbf5f60cb9a in [@inline_let] let rY : S.felem = 0x1eddaf51836859e1369f1ae8d9ab02e4123b6f151d9b796e297a38fa5613d9bc in [@inline_let] let rZ : S.felem = 0xcb433ab3f67815707e398dc7910cc4ec6ea115360060fc73c35b53dce02e2c72 in (rX, rY, rZ) val lemma_proj_g_pow2_192_eval : unit -> Lemma (SE.exp_pow2 S.mk_p256_concrete_ops proj_g_pow2_128 64 == proj_g_pow2_192) let lemma_proj_g_pow2_192_eval () = SPT256.exp_pow2_rec_is_exp_pow2 S.mk_p256_concrete_ops proj_g_pow2_128 64; let qX, qY, qZ = normalize_term (SPT256.exp_pow2_rec S.mk_p256_concrete_ops proj_g_pow2_128 64) in normalize_term_spec (SPT256.exp_pow2_rec S.mk_p256_concrete_ops proj_g_pow2_128 64); let rX : S.felem = 0xc762a9c8ae1b2f7434ff8da70fe105e0d4f188594989f193de0dbdbf5f60cb9a in let rY : S.felem = 0x1eddaf51836859e1369f1ae8d9ab02e4123b6f151d9b796e297a38fa5613d9bc in let rZ : S.felem = 0xcb433ab3f67815707e398dc7910cc4ec6ea115360060fc73c35b53dce02e2c72 in assert_norm (qX == rX /\ qY == rY /\ qZ == rZ) // let proj_g_pow2_64 : S.proj_point = // normalize_term (SPT256.exp_pow2_rec S.mk_p256_concrete_ops S.base_point 64) // let proj_g_pow2_128 : S.proj_point = // normalize_term (SPT256.exp_pow2_rec S.mk_p256_concrete_ops proj_g_pow2_64 64) // let proj_g_pow2_192 : S.proj_point = // normalize_term (SPT256.exp_pow2_rec S.mk_p256_concrete_ops proj_g_pow2_128 64) inline_for_extraction noextract let proj_g_pow2_64_list : SPTK.point_list = normalize_term (SPTK.proj_point_to_list proj_g_pow2_64) inline_for_extraction noextract let proj_g_pow2_128_list : SPTK.point_list = normalize_term (SPTK.proj_point_to_list proj_g_pow2_128) inline_for_extraction noextract let proj_g_pow2_192_list : SPTK.point_list = normalize_term (SPTK.proj_point_to_list proj_g_pow2_192) let proj_g_pow2_64_lseq : LSeq.lseq uint64 12 = normalize_term_spec (SPTK.proj_point_to_list proj_g_pow2_64); Seq.seq_of_list proj_g_pow2_64_list let proj_g_pow2_128_lseq : LSeq.lseq uint64 12 = normalize_term_spec (SPTK.proj_point_to_list proj_g_pow2_128); Seq.seq_of_list proj_g_pow2_128_list let proj_g_pow2_192_lseq : LSeq.lseq uint64 12 = normalize_term_spec (SPTK.proj_point_to_list proj_g_pow2_192); Seq.seq_of_list proj_g_pow2_192_list val proj_g_pow2_64_lemma: unit -> Lemma (S.to_aff_point proj_g_pow2_64 == pow_point (pow2 64) g_aff) let proj_g_pow2_64_lemma () = lemma_proj_g_pow2_64_eval (); SPT256.a_pow2_64_lemma S.mk_p256_concrete_ops S.base_point val proj_g_pow2_128_lemma: unit -> Lemma (S.to_aff_point proj_g_pow2_128 == pow_point (pow2 128) g_aff) let proj_g_pow2_128_lemma () = lemma_proj_g_pow2_128_eval (); lemma_proj_g_pow2_64_eval (); SPT256.a_pow2_128_lemma S.mk_p256_concrete_ops S.base_point val proj_g_pow2_192_lemma: unit -> Lemma (S.to_aff_point proj_g_pow2_192 == pow_point (pow2 192) g_aff) let proj_g_pow2_192_lemma () = lemma_proj_g_pow2_192_eval (); lemma_proj_g_pow2_128_eval (); lemma_proj_g_pow2_64_eval (); SPT256.a_pow2_192_lemma S.mk_p256_concrete_ops S.base_point let proj_g_pow2_64_lseq_lemma () = normalize_term_spec (SPTK.proj_point_to_list proj_g_pow2_64); proj_g_pow2_64_lemma (); SPTK.proj_point_to_list_lemma proj_g_pow2_64 let proj_g_pow2_128_lseq_lemma () = normalize_term_spec (SPTK.proj_point_to_list proj_g_pow2_128); proj_g_pow2_128_lemma (); SPTK.proj_point_to_list_lemma proj_g_pow2_128 let proj_g_pow2_192_lseq_lemma () = normalize_term_spec (SPTK.proj_point_to_list proj_g_pow2_192); proj_g_pow2_192_lemma (); SPTK.proj_point_to_list_lemma proj_g_pow2_192 let mk_proj_g_pow2_64 () = createL proj_g_pow2_64_list let mk_proj_g_pow2_128 () = createL proj_g_pow2_128_list let mk_proj_g_pow2_192 () = createL proj_g_pow2_192_list //---------------- /// window size = 4; precomputed table = [[0]G, [1]G, ..., [15]G] inline_for_extraction noextract let precomp_basepoint_table_list_w4: x:list uint64{FStar.List.Tot.length x = 192} = normalize_term (SPT.precomp_base_table_list mk_p256_precomp_base_table S.base_point 15)
{ "checked_file": "/", "dependencies": [ "Spec.P256.Lemmas.fsti.checked", "Spec.P256.fst.checked", "Spec.Exponentiation.fsti.checked", "prims.fst.checked", "Lib.Sequence.fsti.checked", "Lib.IntTypes.fsti.checked", "Lib.Exponentiation.fsti.checked", "Lib.Buffer.fsti.checked", "Hacl.Spec.PrecompBaseTable256.fsti.checked", "Hacl.Spec.PrecompBaseTable.fsti.checked", "Hacl.Spec.P256.PrecompTable.fsti.checked", "Hacl.Impl.P256.Point.fsti.checked", "Hacl.Impl.P256.Group.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked" ], "interface_file": true, "source_file": "Hacl.P256.PrecompTable.fst" }
[ { "abbrev": true, "full_module": "Spec.P256.Lemmas", "short_module": "SL" }, { "abbrev": true, "full_module": "Hacl.Spec.P256.PrecompTable", "short_module": "SPTK" }, { "abbrev": true, "full_module": "Hacl.Spec.PrecompBaseTable256", "short_module": "SPT256" }, { "abbrev": true, "full_module": "Lib.Exponentiation", "short_module": "LE" }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Group", "short_module": null }, { "abbrev": false, "full_module": "Hacl.Impl.P256.Point", "short_module": null }, { "abbrev": true, "full_module": "Hacl.Spec.P256.Montgomery", "short_module": "SM" }, { "abbrev": true, "full_module": "Spec.P256", "short_module": "S" }, { "abbrev": true, "full_module": "Hacl.Spec.PrecompBaseTable", "short_module": "SPT" }, { "abbrev": true, "full_module": "Hacl.Impl.Exponentiation.Definitions", "short_module": "BE" }, { "abbrev": true, "full_module": "Spec.Exponentiation", "short_module": "SE" }, { "abbrev": true, "full_module": "Lib.Exponentiation.Definition", "short_module": "LE" }, { "abbrev": true, "full_module": "Lib.Sequence", "short_module": "LSeq" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "ST" }, { "abbrev": false, "full_module": "Lib.Buffer", "short_module": null }, { "abbrev": false, "full_module": "Lib.IntTypes", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "FStar.HyperStack", "short_module": null }, { "abbrev": false, "full_module": "Hacl.P256", "short_module": null }, { "abbrev": false, "full_module": "Hacl.P256", "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
Lib.Sequence.lseq Lib.IntTypes.uint64 192
Prims.Tot
[ "total" ]
[]
[ "FStar.Seq.Base.seq_of_list", "Lib.IntTypes.int_t", "Lib.IntTypes.U64", "Lib.IntTypes.SEC", "Hacl.P256.PrecompTable.precomp_basepoint_table_list_w4", "Prims.unit", "FStar.Pervasives.normalize_term_spec", "Prims.list", "Lib.IntTypes.uint_t", "Prims.b2t", "Prims.op_Equality", "Prims.int", "FStar.List.Tot.Base.length", "FStar.Mul.op_Star", "Prims.op_Addition", "Lib.IntTypes.v", "Lib.IntTypes.U32", "Lib.IntTypes.PUB", "FStar.UInt32.uint_to_t", "Hacl.Spec.PrecompBaseTable.precomp_base_table_list", "Spec.P256.PointOps.proj_point", "Hacl.P256.PrecompTable.mk_p256_precomp_base_table", "Spec.P256.PointOps.base_point", "Lib.Sequence.lseq", "Lib.IntTypes.uint64" ]
[]
false
false
false
false
false
let precomp_basepoint_table_lseq_w4:LSeq.lseq uint64 192 =
normalize_term_spec (SPT.precomp_base_table_list mk_p256_precomp_base_table S.base_point 15); Seq.seq_of_list precomp_basepoint_table_list_w4
false
LowParseWriters.fsti
LowParseWriters.get_state
val get_state: #inv: memory_invariant -> #p: parser -> Prims.unit -> Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv
val get_state: #inv: memory_invariant -> #p: parser -> Prims.unit -> Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv
let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p))
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 49, "end_line": 1420, "start_col": 0, "start_line": 1415 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> LowParseWriters.Write (FStar.Ghost.erased (Parser?.t p))
LowParseWriters.Write
[]
[]
[ "LowParseWriters.memory_invariant", "LowParseWriters.LowParse.parser", "Prims.unit", "LowParseWriters.Repr", "FStar.Ghost.erased", "LowParseWriters.LowParse.__proj__Parser__item__t", "Prims.l_True", "Prims.l_and", "Prims.eq2", "FStar.Ghost.reveal", "Prims.l_False", "LowParseWriters.get_state_spec", "LowParseWriters.get_state_impl" ]
[]
false
true
false
false
false
let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv =
EWrite?.reflect (Repr _ (get_state_impl inv p))
false
LowParseWriters.fsti
LowParseWriters.start
val start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l
val start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l
let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) ()
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 6, "end_line": 1535, "start_col": 0, "start_line": 1525 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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
w: LowParseWriters.LowParse.leaf_writer p -> x: Parser?.t p -> LowParseWriters.Write Prims.unit
LowParseWriters.Write
[]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.LowParse.leaf_writer", "LowParseWriters.memory_invariant", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.mk_repr", "Prims.unit", "LowParseWriters.LowParse.parse_empty", "Prims.l_True", "Prims.eq2", "Prims.l_False", "LowParseWriters.elem_writer_spec", "LowParseWriters.elem_writer_impl" ]
[]
false
true
false
false
false
let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l =
mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) ()
false
LowParseWriters.fsti
LowParseWriters.frame_spec
val frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: (unit -> EWrite a parse_empty p pre post post_err l)) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err))
val frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: (unit -> EWrite a parse_empty p pre post post_err l)) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err))
let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 22, "end_line": 1467, "start_col": 0, "start_line": 1454 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> frame: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t LowParseWriters.LowParse.parse_empty -> p: LowParseWriters.LowParse.parser -> post: LowParseWriters.post_t a LowParseWriters.LowParse.parse_empty p pre -> post_err: LowParseWriters.post_err_t LowParseWriters.LowParse.parse_empty pre -> l: LowParseWriters.memory_invariant -> inner: (_: Prims.unit -> LowParseWriters.EWrite a) -> LowParseWriters.repr_spec a frame (LowParseWriters.frame_out a frame p) (LowParseWriters.frame_pre frame pre) (LowParseWriters.frame_post a frame pre p post) (LowParseWriters.frame_post_err frame pre post_err)
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.LowParse.parse_empty", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.memory_invariant", "Prims.unit", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.destr_repr_spec", "LowParseWriters.Correct", "FStar.Pervasives.Native.tuple2", "LowParseWriters.frame_out", "FStar.Pervasives.Native.Mktuple2", "Prims.string", "LowParseWriters.Error", "LowParseWriters.result", "LowParseWriters.repr_spec", "LowParseWriters.frame_pre", "LowParseWriters.frame_post", "LowParseWriters.frame_post_err" ]
[]
false
false
false
false
false
let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: (unit -> EWrite a parse_empty p pre post post_err l)) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) =
fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e
false
LowParseWriters.fsti
LowParseWriters.append
val append (#fr #p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l
val append (#fr #p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l
let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 99, "end_line": 1549, "start_col": 0, "start_line": 1542 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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": 64, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
w: LowParseWriters.LowParse.leaf_writer p -> x: Parser?.t p -> LowParseWriters.Write Prims.unit
LowParseWriters.Write
[]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.LowParse.leaf_writer", "LowParseWriters.memory_invariant", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.frame", "Prims.unit", "LowParseWriters.LowParse.parse_empty", "Prims.l_True", "Prims.eq2", "Prims.l_False", "LowParseWriters.start", "LowParseWriters.LowParse.parse_pair", "Prims.l_and" ]
[]
false
true
false
false
false
let append (#fr #p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l =
frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x)
false
LowParseWriters.fsti
LowParseWriters.valid_rewrite_spec
val valid_rewrite_spec (p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 {precond x} -> GTot (Parser?.t p2))) (v: valid_rewrite_t p1 p2 precond f) : Tot (repr_spec unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False))
val valid_rewrite_spec (p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 {precond x} -> GTot (Parser?.t p2))) (v: valid_rewrite_t p1 p2 precond f) : Tot (repr_spec unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False))
let valid_rewrite_spec (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) : Tot (repr_spec unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False)) = fun vin -> v.valid_rewrite_size vin; Correct ((), f vin)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 23, "end_line": 1824, "start_col": 0, "start_line": 1815 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in inline_for_extraction val recast_writer_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f)) inline_for_extraction let recast_writer_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) = mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f) inline_for_extraction let recast_writer (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: unit -> EWrite a r_in r_out pre post post_err l) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l = recast_writer_repr a r_in r_out pre post post_err l f () inline_for_extraction let frame' (a: Type) (fr: parser) (p: parser) (l: memory_invariant) (f: unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l ) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout) ) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ()) ) l = frame _ _ _ _ _ _ _ (fun _ -> recast_writer _ _ _ _ _ _ _ f) unfold let frame2_pre (frame: parser) (ppre: parser) (pre: pre_t ppre) : Tot (pre_t (frame `parse_pair` ppre)) = fun (_, x) -> pre x unfold let frame2_post (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) : Tot (post_t a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre)) = fun (v_frame, v_in) v (v_frame', v_out) -> v_frame == v_frame' /\ post v_in v v_out unfold let frame2_post_err (frame: parser) (ppre: parser) (pre: pre_t ppre) (post_err: post_err_t ppre pre) : Tot (post_err_t (frame `parse_pair` ppre) (frame2_pre frame ppre pre)) = fun (_, x) -> post_err x let frame2_spec (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_spec a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err)) = fun (fr, w_in) -> match destr_repr_spec a ppre p pre post post_err l inner w_in with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame2_impl (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_impl a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner)) inline_for_extraction let frame2_repr (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (unit -> EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l) = mk_repr a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner) (frame2_impl a frame ppre pre p post post_err l inner) inline_for_extraction let frame2 (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) ($inner: unit -> EWrite a ppre p pre post post_err l) : EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l = frame2_repr a frame ppre pre p post post_err l inner () noeq [@erasable] // very important, otherwise KaRaMeL will fail with argument typing type valid_rewrite_t (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) = { valid_rewrite_valid: (h: HS.mem) -> (b: B.buffer U8.t) -> (pos: U32.t) -> (pos' : U32.t) -> Lemma (requires ( valid_pos p1 h b pos pos' /\ precond (contents p1 h b pos pos') )) (ensures ( valid_pos p1 h b pos pos' /\ ( let x = contents p1 h b pos pos' in precond x /\ valid_pos p2 h b pos pos' /\ contents p2 h b pos pos' == f x ))); valid_rewrite_size: (x: Parser?.t p1 { precond x }) -> Lemma (size p1 x == size p2 (f x)) } let valid_rewrite_implies (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (precond' : pre_t p1) (f' : (x: Parser?.t p1 { precond' x }) -> GTot (Parser?.t p2)) : Pure (valid_rewrite_t p1 p2 precond' f') (requires ( (forall (x: Parser?.t p1) . precond' x ==> precond x) /\ (forall (x: Parser?.t p1 { precond' x }) . f' x == f x) )) (ensures (fun _ -> True)) = { valid_rewrite_valid = (fun h b pos pos' -> v.valid_rewrite_valid h b pos pos'); valid_rewrite_size = (fun x -> v.valid_rewrite_size x); } let valid_rewrite_compose (p1: parser) (p2: parser) (precond12: pre_t p1) (f12: (x: Parser?.t p1 { precond12 x }) -> GTot (Parser?.t p2)) (v12: valid_rewrite_t p1 p2 precond12 f12) (p3: parser) (precond23: pre_t p2) (f23: (x: Parser?.t p2 { precond23 x }) -> GTot (Parser?.t p3)) (v23: valid_rewrite_t p2 p3 precond23 f23) : Tot (valid_rewrite_t p1 p3 (fun x -> precond12 x /\ precond23 (f12 x)) (fun x -> f23 (f12 x))) = { valid_rewrite_valid = (fun h b pos pos' -> v12.valid_rewrite_valid h b pos pos'; v23.valid_rewrite_valid h b pos pos' ); valid_rewrite_size = (fun x -> v12.valid_rewrite_size x; v23.valid_rewrite_size (f12 x) ); }
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.LowParse.parser -> p2: LowParseWriters.LowParse.parser -> precond: LowParseWriters.pre_t p1 -> f: (x: Parser?.t p1 {precond x} -> Prims.GTot (Parser?.t p2)) -> v: LowParseWriters.valid_rewrite_t p1 p2 precond f -> LowParseWriters.repr_spec Prims.unit p1 p2 precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> Prims.l_False)
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.valid_rewrite_t", "LowParseWriters.Correct", "FStar.Pervasives.Native.tuple2", "Prims.unit", "FStar.Pervasives.Native.Mktuple2", "LowParseWriters.__proj__Mkvalid_rewrite_t__item__valid_rewrite_size", "LowParseWriters.result", "LowParseWriters.repr_spec", "Prims.l_and", "Prims.eq2", "Prims.l_False" ]
[]
false
false
false
false
false
let valid_rewrite_spec (p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 {precond x} -> GTot (Parser?.t p2))) (v: valid_rewrite_t p1 p2 precond f) : Tot (repr_spec unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False)) =
fun vin -> v.valid_rewrite_size vin; Correct ((), f vin)
false
LowParseWriters.fsti
LowParseWriters.frame_repr
val frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: (unit -> EWrite a parse_empty p pre post post_err l)) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l)
val frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: (unit -> EWrite a parse_empty p pre post post_err l)) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l)
let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 52, "end_line": 1495, "start_col": 0, "start_line": 1482 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> frame: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t LowParseWriters.LowParse.parse_empty -> p: LowParseWriters.LowParse.parser -> post: LowParseWriters.post_t a LowParseWriters.LowParse.parse_empty p pre -> post_err: LowParseWriters.post_err_t LowParseWriters.LowParse.parse_empty pre -> l: LowParseWriters.memory_invariant -> inner: (_: Prims.unit -> LowParseWriters.EWrite a) -> _: Prims.unit -> LowParseWriters.EWrite a
LowParseWriters.EWrite
[]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.LowParse.parse_empty", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.memory_invariant", "Prims.unit", "LowParseWriters.mk_repr", "LowParseWriters.frame_out", "LowParseWriters.frame_pre", "LowParseWriters.frame_post", "LowParseWriters.frame_post_err", "LowParseWriters.frame_spec", "LowParseWriters.frame_impl" ]
[]
false
true
false
false
false
let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: (unit -> EWrite a parse_empty p pre post post_err l)) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) =
mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner)
false
LowParseWriters.fsti
LowParseWriters.frame'
val frame' (a: Type) (fr p: parser) (l: memory_invariant) (f: (unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l)) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout)) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ())) l
val frame' (a: Type) (fr p: parser) (l: memory_invariant) (f: (unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l)) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout)) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ())) l
let frame' (a: Type) (fr: parser) (p: parser) (l: memory_invariant) (f: unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l ) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout) ) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ()) ) l = frame _ _ _ _ _ _ _ (fun _ -> recast_writer _ _ _ _ _ _ _ f)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 62, "end_line": 1653, "start_col": 0, "start_line": 1634 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in inline_for_extraction val recast_writer_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f)) inline_for_extraction let recast_writer_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) = mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f) inline_for_extraction let recast_writer (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: unit -> EWrite a r_in r_out pre post post_err l) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l = recast_writer_repr a r_in r_out pre post post_err l f ()
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> fr: LowParseWriters.LowParse.parser -> p: LowParseWriters.LowParse.parser -> l: LowParseWriters.memory_invariant -> f: (_: Prims.unit -> LowParseWriters.EWrite a) -> LowParseWriters.EWrite a
LowParseWriters.EWrite
[]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.memory_invariant", "Prims.unit", "LowParseWriters.LowParse.parse_empty", "LowParseWriters.LowParse.__proj__Parser__item__t", "Prims.l_True", "LowParseWriters.frame", "Prims.l_and", "Prims.eq2", "LowParseWriters.result", "FStar.Pervasives.Native.tuple2", "LowParseWriters.destr_repr_spec", "LowParseWriters.Correct", "FStar.Pervasives.Native.Mktuple2", "Prims.b2t", "LowParseWriters.uu___is_Error", "LowParseWriters.recast_writer", "LowParseWriters.LowParse.parse_pair" ]
[]
false
true
false
false
false
let frame' (a: Type) (fr p: parser) (l: memory_invariant) (f: (unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l)) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout)) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ())) l =
frame _ _ _ _ _ _ _ (fun _ -> recast_writer _ _ _ _ _ _ _ f)
false
LowParseWriters.fsti
LowParseWriters.valid_rewrite_parse_pair_assoc_2
val valid_rewrite_parse_pair_assoc_2 (p1 p2 p3: parser) : Tot (valid_rewrite_t (p1 `parse_pair` (p2 `parse_pair` p3)) ((p1 `parse_pair` p2) `parse_pair` p3) (fun _ -> True) (fun (x1, (x2, x3)) -> ((x1, x2), x3)))
val valid_rewrite_parse_pair_assoc_2 (p1 p2 p3: parser) : Tot (valid_rewrite_t (p1 `parse_pair` (p2 `parse_pair` p3)) ((p1 `parse_pair` p2) `parse_pair` p3) (fun _ -> True) (fun (x1, (x2, x3)) -> ((x1, x2), x3)))
let valid_rewrite_parse_pair_assoc_2 (p1 p2 p3: parser) : Tot (valid_rewrite_t (p1 `parse_pair` (p2 `parse_pair` p3)) ((p1 `parse_pair` p2) `parse_pair` p3) (fun _ -> True) (fun (x1, (x2, x3)) -> ((x1, x2), x3))) = { valid_rewrite_valid = (fun h b pos pos' -> let pos2 = valid_parse_pair_inv_spec h p1 (p2 `parse_pair` p3) b pos pos' in let pos3 = valid_parse_pair_inv_spec h p2 p3 b pos2 pos' in valid_parse_pair p1 p2 h b pos pos2 pos3; valid_parse_pair (p1 `parse_pair` p2) p3 h b pos pos3 pos' ); valid_rewrite_size = (fun _ -> ()); }
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 1895, "start_col": 0, "start_line": 1884 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in inline_for_extraction val recast_writer_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f)) inline_for_extraction let recast_writer_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) = mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f) inline_for_extraction let recast_writer (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: unit -> EWrite a r_in r_out pre post post_err l) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l = recast_writer_repr a r_in r_out pre post post_err l f () inline_for_extraction let frame' (a: Type) (fr: parser) (p: parser) (l: memory_invariant) (f: unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l ) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout) ) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ()) ) l = frame _ _ _ _ _ _ _ (fun _ -> recast_writer _ _ _ _ _ _ _ f) unfold let frame2_pre (frame: parser) (ppre: parser) (pre: pre_t ppre) : Tot (pre_t (frame `parse_pair` ppre)) = fun (_, x) -> pre x unfold let frame2_post (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) : Tot (post_t a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre)) = fun (v_frame, v_in) v (v_frame', v_out) -> v_frame == v_frame' /\ post v_in v v_out unfold let frame2_post_err (frame: parser) (ppre: parser) (pre: pre_t ppre) (post_err: post_err_t ppre pre) : Tot (post_err_t (frame `parse_pair` ppre) (frame2_pre frame ppre pre)) = fun (_, x) -> post_err x let frame2_spec (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_spec a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err)) = fun (fr, w_in) -> match destr_repr_spec a ppre p pre post post_err l inner w_in with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame2_impl (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_impl a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner)) inline_for_extraction let frame2_repr (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (unit -> EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l) = mk_repr a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner) (frame2_impl a frame ppre pre p post post_err l inner) inline_for_extraction let frame2 (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) ($inner: unit -> EWrite a ppre p pre post post_err l) : EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l = frame2_repr a frame ppre pre p post post_err l inner () noeq [@erasable] // very important, otherwise KaRaMeL will fail with argument typing type valid_rewrite_t (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) = { valid_rewrite_valid: (h: HS.mem) -> (b: B.buffer U8.t) -> (pos: U32.t) -> (pos' : U32.t) -> Lemma (requires ( valid_pos p1 h b pos pos' /\ precond (contents p1 h b pos pos') )) (ensures ( valid_pos p1 h b pos pos' /\ ( let x = contents p1 h b pos pos' in precond x /\ valid_pos p2 h b pos pos' /\ contents p2 h b pos pos' == f x ))); valid_rewrite_size: (x: Parser?.t p1 { precond x }) -> Lemma (size p1 x == size p2 (f x)) } let valid_rewrite_implies (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (precond' : pre_t p1) (f' : (x: Parser?.t p1 { precond' x }) -> GTot (Parser?.t p2)) : Pure (valid_rewrite_t p1 p2 precond' f') (requires ( (forall (x: Parser?.t p1) . precond' x ==> precond x) /\ (forall (x: Parser?.t p1 { precond' x }) . f' x == f x) )) (ensures (fun _ -> True)) = { valid_rewrite_valid = (fun h b pos pos' -> v.valid_rewrite_valid h b pos pos'); valid_rewrite_size = (fun x -> v.valid_rewrite_size x); } let valid_rewrite_compose (p1: parser) (p2: parser) (precond12: pre_t p1) (f12: (x: Parser?.t p1 { precond12 x }) -> GTot (Parser?.t p2)) (v12: valid_rewrite_t p1 p2 precond12 f12) (p3: parser) (precond23: pre_t p2) (f23: (x: Parser?.t p2 { precond23 x }) -> GTot (Parser?.t p3)) (v23: valid_rewrite_t p2 p3 precond23 f23) : Tot (valid_rewrite_t p1 p3 (fun x -> precond12 x /\ precond23 (f12 x)) (fun x -> f23 (f12 x))) = { valid_rewrite_valid = (fun h b pos pos' -> v12.valid_rewrite_valid h b pos pos'; v23.valid_rewrite_valid h b pos pos' ); valid_rewrite_size = (fun x -> v12.valid_rewrite_size x; v23.valid_rewrite_size (f12 x) ); } let valid_rewrite_spec (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) : Tot (repr_spec unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False)) = fun vin -> v.valid_rewrite_size vin; Correct ((), f vin) inline_for_extraction val valid_rewrite_impl (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) : Tot (repr_impl unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False) inv (valid_rewrite_spec p1 p2 precond f v)) inline_for_extraction let valid_rewrite_repr (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) : Tot (repr unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False) inv) = Repr _ (valid_rewrite_impl p1 p2 precond f v inv) inline_for_extraction let valid_rewrite (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (inv: memory_invariant) (v: valid_rewrite_t p1 p2 precond f) : Write unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) inv = EWrite?.reflect (valid_rewrite_repr p1 p2 precond f v inv) inline_for_extraction val cast (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) (x1: ptr p1 inv { precond (deref_spec x1) }) : Tot (x2: ptr p2 inv { deref_spec x2 == f (deref_spec x1) }) let valid_rewrite_parse_pair_assoc_1 (p1 p2 p3: parser) : Tot (valid_rewrite_t ((p1 `parse_pair` p2) `parse_pair` p3) (p1 `parse_pair` (p2 `parse_pair` p3)) (fun _ -> True) (fun ((x1, x2), x3) -> (x1, (x2, x3)))) = { valid_rewrite_valid = (fun h b pos pos' -> let pos3 = valid_parse_pair_inv_spec h (p1 `parse_pair` p2) p3 b pos pos' in let pos2 = valid_parse_pair_inv_spec h p1 p2 b pos pos3 in valid_parse_pair p2 p3 h b pos2 pos3 pos'; valid_parse_pair p1 (p2 `parse_pair` p3) h b pos pos2 pos' ); valid_rewrite_size = (fun _ -> ()); }
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.LowParse.parser -> p2: LowParseWriters.LowParse.parser -> p3: LowParseWriters.LowParse.parser -> LowParseWriters.valid_rewrite_t (LowParseWriters.LowParse.parse_pair p1 (LowParseWriters.LowParse.parse_pair p2 p3)) (LowParseWriters.LowParse.parse_pair (LowParseWriters.LowParse.parse_pair p1 p2) p3) (fun _ -> Prims.l_True) (fun _ -> (let FStar.Pervasives.Native.Mktuple2 #_ #_ x1 (FStar.Pervasives.Native.Mktuple2 #_ #_ x2 x3) = _ in (x1, x2), x3) <: Parser?.t (LowParseWriters.LowParse.parse_pair (LowParseWriters.LowParse.parse_pair p1 p2) p3))
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.Mkvalid_rewrite_t", "LowParseWriters.LowParse.parse_pair", "LowParseWriters.LowParse.__proj__Parser__item__t", "Prims.l_True", "FStar.Pervasives.Native.Mktuple2", "FStar.Monotonic.HyperStack.mem", "LowStar.Buffer.buffer", "FStar.UInt8.t", "FStar.UInt32.t", "LowParseWriters.LowParse.valid_parse_pair", "Prims.unit", "LowParseWriters.LowParse.valid_parse_pair_inv_spec", "LowParseWriters.valid_rewrite_t" ]
[]
false
false
false
false
false
let valid_rewrite_parse_pair_assoc_2 (p1 p2 p3: parser) : Tot (valid_rewrite_t (p1 `parse_pair` (p2 `parse_pair` p3)) ((p1 `parse_pair` p2) `parse_pair` p3) (fun _ -> True) (fun (x1, (x2, x3)) -> ((x1, x2), x3))) =
{ valid_rewrite_valid = (fun h b pos pos' -> let pos2 = valid_parse_pair_inv_spec h p1 (p2 `parse_pair` p3) b pos pos' in let pos3 = valid_parse_pair_inv_spec h p2 p3 b pos2 pos' in valid_parse_pair p1 p2 h b pos pos2 pos3; valid_parse_pair (p1 `parse_pair` p2) p3 h b pos pos3 pos'); valid_rewrite_size = (fun _ -> ()) }
false
LowParseWriters.fsti
LowParseWriters.valid_rewrite_repr
val valid_rewrite_repr (p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 {precond x} -> GTot (Parser?.t p2))) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) : Tot (repr unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False) inv )
val valid_rewrite_repr (p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 {precond x} -> GTot (Parser?.t p2))) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) : Tot (repr unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False) inv )
let valid_rewrite_repr (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) : Tot (repr unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False) inv) = Repr _ (valid_rewrite_impl p1 p2 precond f v inv)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 51, "end_line": 1845, "start_col": 0, "start_line": 1837 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in inline_for_extraction val recast_writer_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f)) inline_for_extraction let recast_writer_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) = mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f) inline_for_extraction let recast_writer (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: unit -> EWrite a r_in r_out pre post post_err l) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l = recast_writer_repr a r_in r_out pre post post_err l f () inline_for_extraction let frame' (a: Type) (fr: parser) (p: parser) (l: memory_invariant) (f: unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l ) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout) ) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ()) ) l = frame _ _ _ _ _ _ _ (fun _ -> recast_writer _ _ _ _ _ _ _ f) unfold let frame2_pre (frame: parser) (ppre: parser) (pre: pre_t ppre) : Tot (pre_t (frame `parse_pair` ppre)) = fun (_, x) -> pre x unfold let frame2_post (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) : Tot (post_t a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre)) = fun (v_frame, v_in) v (v_frame', v_out) -> v_frame == v_frame' /\ post v_in v v_out unfold let frame2_post_err (frame: parser) (ppre: parser) (pre: pre_t ppre) (post_err: post_err_t ppre pre) : Tot (post_err_t (frame `parse_pair` ppre) (frame2_pre frame ppre pre)) = fun (_, x) -> post_err x let frame2_spec (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_spec a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err)) = fun (fr, w_in) -> match destr_repr_spec a ppre p pre post post_err l inner w_in with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame2_impl (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_impl a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner)) inline_for_extraction let frame2_repr (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (unit -> EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l) = mk_repr a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner) (frame2_impl a frame ppre pre p post post_err l inner) inline_for_extraction let frame2 (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) ($inner: unit -> EWrite a ppre p pre post post_err l) : EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l = frame2_repr a frame ppre pre p post post_err l inner () noeq [@erasable] // very important, otherwise KaRaMeL will fail with argument typing type valid_rewrite_t (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) = { valid_rewrite_valid: (h: HS.mem) -> (b: B.buffer U8.t) -> (pos: U32.t) -> (pos' : U32.t) -> Lemma (requires ( valid_pos p1 h b pos pos' /\ precond (contents p1 h b pos pos') )) (ensures ( valid_pos p1 h b pos pos' /\ ( let x = contents p1 h b pos pos' in precond x /\ valid_pos p2 h b pos pos' /\ contents p2 h b pos pos' == f x ))); valid_rewrite_size: (x: Parser?.t p1 { precond x }) -> Lemma (size p1 x == size p2 (f x)) } let valid_rewrite_implies (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (precond' : pre_t p1) (f' : (x: Parser?.t p1 { precond' x }) -> GTot (Parser?.t p2)) : Pure (valid_rewrite_t p1 p2 precond' f') (requires ( (forall (x: Parser?.t p1) . precond' x ==> precond x) /\ (forall (x: Parser?.t p1 { precond' x }) . f' x == f x) )) (ensures (fun _ -> True)) = { valid_rewrite_valid = (fun h b pos pos' -> v.valid_rewrite_valid h b pos pos'); valid_rewrite_size = (fun x -> v.valid_rewrite_size x); } let valid_rewrite_compose (p1: parser) (p2: parser) (precond12: pre_t p1) (f12: (x: Parser?.t p1 { precond12 x }) -> GTot (Parser?.t p2)) (v12: valid_rewrite_t p1 p2 precond12 f12) (p3: parser) (precond23: pre_t p2) (f23: (x: Parser?.t p2 { precond23 x }) -> GTot (Parser?.t p3)) (v23: valid_rewrite_t p2 p3 precond23 f23) : Tot (valid_rewrite_t p1 p3 (fun x -> precond12 x /\ precond23 (f12 x)) (fun x -> f23 (f12 x))) = { valid_rewrite_valid = (fun h b pos pos' -> v12.valid_rewrite_valid h b pos pos'; v23.valid_rewrite_valid h b pos pos' ); valid_rewrite_size = (fun x -> v12.valid_rewrite_size x; v23.valid_rewrite_size (f12 x) ); } let valid_rewrite_spec (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) : Tot (repr_spec unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False)) = fun vin -> v.valid_rewrite_size vin; Correct ((), f vin) inline_for_extraction val valid_rewrite_impl (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) : Tot (repr_impl unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False) inv (valid_rewrite_spec p1 p2 precond f v))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.LowParse.parser -> p2: LowParseWriters.LowParse.parser -> precond: LowParseWriters.pre_t p1 -> f: (x: Parser?.t p1 {precond x} -> Prims.GTot (Parser?.t p2)) -> v: LowParseWriters.valid_rewrite_t p1 p2 precond f -> inv: LowParseWriters.memory_invariant -> LowParseWriters.repr Prims.unit p1 p2 precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> Prims.l_False) inv
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.valid_rewrite_t", "LowParseWriters.memory_invariant", "LowParseWriters.Repr", "Prims.unit", "Prims.l_and", "Prims.eq2", "Prims.l_False", "LowParseWriters.valid_rewrite_spec", "LowParseWriters.valid_rewrite_impl", "LowParseWriters.repr" ]
[]
false
false
false
false
false
let valid_rewrite_repr (p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 {precond x} -> GTot (Parser?.t p2))) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) : Tot (repr unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False) inv ) =
Repr _ (valid_rewrite_impl p1 p2 precond f v inv)
false
LowParseWriters.fsti
LowParseWriters.recast_writer_spec
val recast_writer_spec (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f))
val recast_writer_spec (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f))
let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 69, "end_line": 1591, "start_col": 0, "start_line": 1581 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> r_in: LowParseWriters.LowParse.parser -> r_out: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t r_in -> post: LowParseWriters.post_t a r_in r_out pre -> post_err: LowParseWriters.post_err_t r_in pre -> l: LowParseWriters.memory_invariant -> f: (_: Prims.unit -> LowParseWriters.EWrite a) -> LowParseWriters.repr_spec a r_in r_out pre (LowParseWriters.recast_writer_post a r_in r_out pre post post_err l f) (LowParseWriters.recast_writer_post_err a r_in r_out pre post post_err l f)
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.memory_invariant", "Prims.unit", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.destr_repr_spec", "LowParseWriters.result", "FStar.Pervasives.Native.tuple2", "LowParseWriters.repr_spec", "LowParseWriters.recast_writer_post", "LowParseWriters.recast_writer_post_err" ]
[]
false
false
false
false
false
let recast_writer_spec (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) =
fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in
false
LowParseWriters.fsti
LowParseWriters.valid_rewrite_implies
val valid_rewrite_implies (p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 {precond x} -> GTot (Parser?.t p2))) (v: valid_rewrite_t p1 p2 precond f) (precond': pre_t p1) (f': (x: Parser?.t p1 {precond' x} -> GTot (Parser?.t p2))) : Pure (valid_rewrite_t p1 p2 precond' f') (requires ((forall (x: Parser?.t p1). precond' x ==> precond x) /\ (forall (x: Parser?.t p1 {precond' x}). f' x == f x))) (ensures (fun _ -> True))
val valid_rewrite_implies (p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 {precond x} -> GTot (Parser?.t p2))) (v: valid_rewrite_t p1 p2 precond f) (precond': pre_t p1) (f': (x: Parser?.t p1 {precond' x} -> GTot (Parser?.t p2))) : Pure (valid_rewrite_t p1 p2 precond' f') (requires ((forall (x: Parser?.t p1). precond' x ==> precond x) /\ (forall (x: Parser?.t p1 {precond' x}). f' x == f x))) (ensures (fun _ -> True))
let valid_rewrite_implies (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (precond' : pre_t p1) (f' : (x: Parser?.t p1 { precond' x }) -> GTot (Parser?.t p2)) : Pure (valid_rewrite_t p1 p2 precond' f') (requires ( (forall (x: Parser?.t p1) . precond' x ==> precond x) /\ (forall (x: Parser?.t p1 { precond' x }) . f' x == f x) )) (ensures (fun _ -> True)) = { valid_rewrite_valid = (fun h b pos pos' -> v.valid_rewrite_valid h b pos pos'); valid_rewrite_size = (fun x -> v.valid_rewrite_size x); }
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 1, "end_line": 1791, "start_col": 0, "start_line": 1774 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in inline_for_extraction val recast_writer_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f)) inline_for_extraction let recast_writer_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) = mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f) inline_for_extraction let recast_writer (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: unit -> EWrite a r_in r_out pre post post_err l) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l = recast_writer_repr a r_in r_out pre post post_err l f () inline_for_extraction let frame' (a: Type) (fr: parser) (p: parser) (l: memory_invariant) (f: unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l ) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout) ) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ()) ) l = frame _ _ _ _ _ _ _ (fun _ -> recast_writer _ _ _ _ _ _ _ f) unfold let frame2_pre (frame: parser) (ppre: parser) (pre: pre_t ppre) : Tot (pre_t (frame `parse_pair` ppre)) = fun (_, x) -> pre x unfold let frame2_post (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) : Tot (post_t a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre)) = fun (v_frame, v_in) v (v_frame', v_out) -> v_frame == v_frame' /\ post v_in v v_out unfold let frame2_post_err (frame: parser) (ppre: parser) (pre: pre_t ppre) (post_err: post_err_t ppre pre) : Tot (post_err_t (frame `parse_pair` ppre) (frame2_pre frame ppre pre)) = fun (_, x) -> post_err x let frame2_spec (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_spec a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err)) = fun (fr, w_in) -> match destr_repr_spec a ppre p pre post post_err l inner w_in with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame2_impl (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_impl a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner)) inline_for_extraction let frame2_repr (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (unit -> EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l) = mk_repr a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner) (frame2_impl a frame ppre pre p post post_err l inner) inline_for_extraction let frame2 (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) ($inner: unit -> EWrite a ppre p pre post post_err l) : EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l = frame2_repr a frame ppre pre p post post_err l inner () noeq [@erasable] // very important, otherwise KaRaMeL will fail with argument typing type valid_rewrite_t (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) = { valid_rewrite_valid: (h: HS.mem) -> (b: B.buffer U8.t) -> (pos: U32.t) -> (pos' : U32.t) -> Lemma (requires ( valid_pos p1 h b pos pos' /\ precond (contents p1 h b pos pos') )) (ensures ( valid_pos p1 h b pos pos' /\ ( let x = contents p1 h b pos pos' in precond x /\ valid_pos p2 h b pos pos' /\ contents p2 h b pos pos' == f x ))); valid_rewrite_size: (x: Parser?.t p1 { precond x }) -> Lemma (size p1 x == size p2 (f x)) }
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.LowParse.parser -> p2: LowParseWriters.LowParse.parser -> precond: LowParseWriters.pre_t p1 -> f: (x: Parser?.t p1 {precond x} -> Prims.GTot (Parser?.t p2)) -> v: LowParseWriters.valid_rewrite_t p1 p2 precond f -> precond': LowParseWriters.pre_t p1 -> f': (x: Parser?.t p1 {precond' x} -> Prims.GTot (Parser?.t p2)) -> Prims.Pure (LowParseWriters.valid_rewrite_t p1 p2 precond' f')
Prims.Pure
[]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.valid_rewrite_t", "LowParseWriters.Mkvalid_rewrite_t", "FStar.Monotonic.HyperStack.mem", "LowStar.Buffer.buffer", "FStar.UInt8.t", "FStar.UInt32.t", "LowParseWriters.__proj__Mkvalid_rewrite_t__item__valid_rewrite_valid", "Prims.unit", "LowParseWriters.__proj__Mkvalid_rewrite_t__item__valid_rewrite_size", "Prims.l_and", "Prims.l_Forall", "Prims.l_imp", "Prims.eq2", "Prims.l_True" ]
[]
false
false
false
false
false
let valid_rewrite_implies (p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 {precond x} -> GTot (Parser?.t p2))) (v: valid_rewrite_t p1 p2 precond f) (precond': pre_t p1) (f': (x: Parser?.t p1 {precond' x} -> GTot (Parser?.t p2))) : Pure (valid_rewrite_t p1 p2 precond' f') (requires ((forall (x: Parser?.t p1). precond' x ==> precond x) /\ (forall (x: Parser?.t p1 {precond' x}). f' x == f x))) (ensures (fun _ -> True)) =
{ valid_rewrite_valid = (fun h b pos pos' -> v.valid_rewrite_valid h b pos pos'); valid_rewrite_size = (fun x -> v.valid_rewrite_size x) }
false
LowParseWriters.fsti
LowParseWriters.recast_writer
val recast_writer (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: (unit -> EWrite a r_in r_out pre post post_err l)) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l
val recast_writer (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: (unit -> EWrite a r_in r_out pre post post_err l)) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l
let recast_writer (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: unit -> EWrite a r_in r_out pre post post_err l) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l = recast_writer_repr a r_in r_out pre post post_err l f ()
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 58, "end_line": 1631, "start_col": 0, "start_line": 1621 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in inline_for_extraction val recast_writer_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f)) inline_for_extraction let recast_writer_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) = mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> r_in: LowParseWriters.LowParse.parser -> r_out: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t r_in -> post: LowParseWriters.post_t a r_in r_out pre -> post_err: LowParseWriters.post_err_t r_in pre -> l: LowParseWriters.memory_invariant -> $f: (_: Prims.unit -> LowParseWriters.EWrite a) -> LowParseWriters.EWrite a
LowParseWriters.EWrite
[]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.memory_invariant", "Prims.unit", "LowParseWriters.recast_writer_repr", "LowParseWriters.recast_writer_post", "LowParseWriters.recast_writer_post_err" ]
[]
false
true
false
false
false
let recast_writer (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: (unit -> EWrite a r_in r_out pre post post_err l)) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l =
recast_writer_repr a r_in r_out pre post post_err l f ()
false
LowParseWriters.fsti
LowParseWriters.check_precond
val check_precond (p1: parser) (precond: pre_t p1) (c: check_precond_t p1 precond) (inv: memory_invariant) : EWrite unit p1 (p1) precond (fun vin _ vout -> vin == vout /\ precond vin) (fun vin -> ~(precond vin)) inv
val check_precond (p1: parser) (precond: pre_t p1) (c: check_precond_t p1 precond) (inv: memory_invariant) : EWrite unit p1 (p1) precond (fun vin _ vout -> vin == vout /\ precond vin) (fun vin -> ~(precond vin)) inv
let check_precond (p1: parser) (precond: pre_t p1) (c: check_precond_t p1 precond) (inv: memory_invariant) : EWrite unit p1 (p1) precond (fun vin _ vout -> vin == vout /\ precond vin) (fun vin -> ~ (precond vin)) inv = EWrite?.reflect (check_precond_repr p1 precond c inv)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 55, "end_line": 1968, "start_col": 0, "start_line": 1962 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in inline_for_extraction val recast_writer_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f)) inline_for_extraction let recast_writer_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) = mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f) inline_for_extraction let recast_writer (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: unit -> EWrite a r_in r_out pre post post_err l) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l = recast_writer_repr a r_in r_out pre post post_err l f () inline_for_extraction let frame' (a: Type) (fr: parser) (p: parser) (l: memory_invariant) (f: unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l ) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout) ) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ()) ) l = frame _ _ _ _ _ _ _ (fun _ -> recast_writer _ _ _ _ _ _ _ f) unfold let frame2_pre (frame: parser) (ppre: parser) (pre: pre_t ppre) : Tot (pre_t (frame `parse_pair` ppre)) = fun (_, x) -> pre x unfold let frame2_post (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) : Tot (post_t a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre)) = fun (v_frame, v_in) v (v_frame', v_out) -> v_frame == v_frame' /\ post v_in v v_out unfold let frame2_post_err (frame: parser) (ppre: parser) (pre: pre_t ppre) (post_err: post_err_t ppre pre) : Tot (post_err_t (frame `parse_pair` ppre) (frame2_pre frame ppre pre)) = fun (_, x) -> post_err x let frame2_spec (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_spec a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err)) = fun (fr, w_in) -> match destr_repr_spec a ppre p pre post post_err l inner w_in with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame2_impl (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_impl a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner)) inline_for_extraction let frame2_repr (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (unit -> EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l) = mk_repr a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner) (frame2_impl a frame ppre pre p post post_err l inner) inline_for_extraction let frame2 (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) ($inner: unit -> EWrite a ppre p pre post post_err l) : EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l = frame2_repr a frame ppre pre p post post_err l inner () noeq [@erasable] // very important, otherwise KaRaMeL will fail with argument typing type valid_rewrite_t (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) = { valid_rewrite_valid: (h: HS.mem) -> (b: B.buffer U8.t) -> (pos: U32.t) -> (pos' : U32.t) -> Lemma (requires ( valid_pos p1 h b pos pos' /\ precond (contents p1 h b pos pos') )) (ensures ( valid_pos p1 h b pos pos' /\ ( let x = contents p1 h b pos pos' in precond x /\ valid_pos p2 h b pos pos' /\ contents p2 h b pos pos' == f x ))); valid_rewrite_size: (x: Parser?.t p1 { precond x }) -> Lemma (size p1 x == size p2 (f x)) } let valid_rewrite_implies (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (precond' : pre_t p1) (f' : (x: Parser?.t p1 { precond' x }) -> GTot (Parser?.t p2)) : Pure (valid_rewrite_t p1 p2 precond' f') (requires ( (forall (x: Parser?.t p1) . precond' x ==> precond x) /\ (forall (x: Parser?.t p1 { precond' x }) . f' x == f x) )) (ensures (fun _ -> True)) = { valid_rewrite_valid = (fun h b pos pos' -> v.valid_rewrite_valid h b pos pos'); valid_rewrite_size = (fun x -> v.valid_rewrite_size x); } let valid_rewrite_compose (p1: parser) (p2: parser) (precond12: pre_t p1) (f12: (x: Parser?.t p1 { precond12 x }) -> GTot (Parser?.t p2)) (v12: valid_rewrite_t p1 p2 precond12 f12) (p3: parser) (precond23: pre_t p2) (f23: (x: Parser?.t p2 { precond23 x }) -> GTot (Parser?.t p3)) (v23: valid_rewrite_t p2 p3 precond23 f23) : Tot (valid_rewrite_t p1 p3 (fun x -> precond12 x /\ precond23 (f12 x)) (fun x -> f23 (f12 x))) = { valid_rewrite_valid = (fun h b pos pos' -> v12.valid_rewrite_valid h b pos pos'; v23.valid_rewrite_valid h b pos pos' ); valid_rewrite_size = (fun x -> v12.valid_rewrite_size x; v23.valid_rewrite_size (f12 x) ); } let valid_rewrite_spec (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) : Tot (repr_spec unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False)) = fun vin -> v.valid_rewrite_size vin; Correct ((), f vin) inline_for_extraction val valid_rewrite_impl (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) : Tot (repr_impl unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False) inv (valid_rewrite_spec p1 p2 precond f v)) inline_for_extraction let valid_rewrite_repr (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) : Tot (repr unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False) inv) = Repr _ (valid_rewrite_impl p1 p2 precond f v inv) inline_for_extraction let valid_rewrite (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (inv: memory_invariant) (v: valid_rewrite_t p1 p2 precond f) : Write unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) inv = EWrite?.reflect (valid_rewrite_repr p1 p2 precond f v inv) inline_for_extraction val cast (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) (x1: ptr p1 inv { precond (deref_spec x1) }) : Tot (x2: ptr p2 inv { deref_spec x2 == f (deref_spec x1) }) let valid_rewrite_parse_pair_assoc_1 (p1 p2 p3: parser) : Tot (valid_rewrite_t ((p1 `parse_pair` p2) `parse_pair` p3) (p1 `parse_pair` (p2 `parse_pair` p3)) (fun _ -> True) (fun ((x1, x2), x3) -> (x1, (x2, x3)))) = { valid_rewrite_valid = (fun h b pos pos' -> let pos3 = valid_parse_pair_inv_spec h (p1 `parse_pair` p2) p3 b pos pos' in let pos2 = valid_parse_pair_inv_spec h p1 p2 b pos pos3 in valid_parse_pair p2 p3 h b pos2 pos3 pos'; valid_parse_pair p1 (p2 `parse_pair` p3) h b pos pos2 pos' ); valid_rewrite_size = (fun _ -> ()); } let valid_rewrite_parse_pair_assoc_2 (p1 p2 p3: parser) : Tot (valid_rewrite_t (p1 `parse_pair` (p2 `parse_pair` p3)) ((p1 `parse_pair` p2) `parse_pair` p3) (fun _ -> True) (fun (x1, (x2, x3)) -> ((x1, x2), x3))) = { valid_rewrite_valid = (fun h b pos pos' -> let pos2 = valid_parse_pair_inv_spec h p1 (p2 `parse_pair` p3) b pos pos' in let pos3 = valid_parse_pair_inv_spec h p2 p3 b pos2 pos' in valid_parse_pair p1 p2 h b pos pos2 pos3; valid_parse_pair (p1 `parse_pair` p2) p3 h b pos pos3 pos' ); valid_rewrite_size = (fun _ -> ()); } let valid_rewrite_parse_pair_compat_l (p: parser) (p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) ($v: valid_rewrite_t p1 p2 precond f) : Tot (valid_rewrite_t (p `parse_pair` p1) (p `parse_pair` p2) (fun (_, x) -> precond x) (fun (x, y) -> (x, f y))) = { valid_rewrite_valid = (fun h b pos pos' -> let posq = valid_parse_pair_inv_spec h p p1 b pos pos' in v.valid_rewrite_valid h b posq pos'; valid_parse_pair p p2 h b pos posq pos' ); valid_rewrite_size = (fun xy -> let (_, x) = (xy <: Parser?.t (p `parse_pair` p1)) in v.valid_rewrite_size x ); } let valid_rewrite_parse_pair_compat_r (p: parser) (p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) ($v: valid_rewrite_t p1 p2 precond f) : Tot (valid_rewrite_t (p1 `parse_pair` p) (p2 `parse_pair` p) (fun (x, _) -> precond x) (fun (x, y) -> (f x, y))) = { valid_rewrite_valid = (fun h b pos pos' -> let posp = valid_parse_pair_inv_spec h p1 p b pos pos' in v.valid_rewrite_valid h b pos posp; valid_parse_pair p2 p h b pos posp pos' ); valid_rewrite_size = (fun xy -> let (x, _) = (xy <: Parser?.t (p1 `parse_pair` p)) in v.valid_rewrite_size x ); } let check_precond_t (p1: parser) (precond: pre_t p1) : Tot Type = (b: B.buffer U8.t) -> (len: U32.t { B.len b == len }) -> (pos: U32.t) -> (pos' : U32.t) -> HST.Stack bool (requires (fun h -> valid_pos p1 h b pos pos' )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ (res == true <==> precond (contents p1 h b pos pos')) )) inline_for_extraction val check_precond_repr (p1: parser) (precond: pre_t p1) (c: check_precond_t p1 precond) (inv: memory_invariant) : Tot (repr unit p1 (p1) precond (fun vin _ vout -> vin == vout /\ precond vin) (fun vin -> ~ (precond vin)) inv)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.LowParse.parser -> precond: LowParseWriters.pre_t p1 -> c: LowParseWriters.check_precond_t p1 precond -> inv: LowParseWriters.memory_invariant -> LowParseWriters.EWrite Prims.unit
LowParseWriters.EWrite
[]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.check_precond_t", "LowParseWriters.memory_invariant", "LowParseWriters.check_precond_repr", "Prims.unit", "LowParseWriters.LowParse.__proj__Parser__item__t", "Prims.l_and", "Prims.eq2", "Prims.l_not" ]
[]
false
true
false
false
false
let check_precond (p1: parser) (precond: pre_t p1) (c: check_precond_t p1 precond) (inv: memory_invariant) : EWrite unit p1 (p1) precond (fun vin _ vout -> vin == vout /\ precond vin) (fun vin -> ~(precond vin)) inv =
EWrite?.reflect (check_precond_repr p1 precond c inv)
false
LowParseWriters.fsti
LowParseWriters.cat
val cat (#inv: memory_invariant) (#p: parser) (x: ptr p inv) : Write unit parse_empty p (fun _ -> True) (fun _ _ vout -> vout == deref_spec x) inv
val cat (#inv: memory_invariant) (#p: parser) (x: ptr p inv) : Write unit parse_empty p (fun _ -> True) (fun _ _ vout -> vout == deref_spec x) inv
let cat (#inv: memory_invariant) (#p: parser) (x: ptr p inv) : Write unit parse_empty p (fun _ -> True) (fun _ _ vout -> vout == deref_spec x) inv = EWrite?.reflect (Repr _ (cat_impl x))
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 39, "end_line": 1990, "start_col": 0, "start_line": 1985 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in inline_for_extraction val recast_writer_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f)) inline_for_extraction let recast_writer_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) = mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f) inline_for_extraction let recast_writer (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: unit -> EWrite a r_in r_out pre post post_err l) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l = recast_writer_repr a r_in r_out pre post post_err l f () inline_for_extraction let frame' (a: Type) (fr: parser) (p: parser) (l: memory_invariant) (f: unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l ) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout) ) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ()) ) l = frame _ _ _ _ _ _ _ (fun _ -> recast_writer _ _ _ _ _ _ _ f) unfold let frame2_pre (frame: parser) (ppre: parser) (pre: pre_t ppre) : Tot (pre_t (frame `parse_pair` ppre)) = fun (_, x) -> pre x unfold let frame2_post (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) : Tot (post_t a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre)) = fun (v_frame, v_in) v (v_frame', v_out) -> v_frame == v_frame' /\ post v_in v v_out unfold let frame2_post_err (frame: parser) (ppre: parser) (pre: pre_t ppre) (post_err: post_err_t ppre pre) : Tot (post_err_t (frame `parse_pair` ppre) (frame2_pre frame ppre pre)) = fun (_, x) -> post_err x let frame2_spec (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_spec a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err)) = fun (fr, w_in) -> match destr_repr_spec a ppre p pre post post_err l inner w_in with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame2_impl (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_impl a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner)) inline_for_extraction let frame2_repr (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (unit -> EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l) = mk_repr a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner) (frame2_impl a frame ppre pre p post post_err l inner) inline_for_extraction let frame2 (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) ($inner: unit -> EWrite a ppre p pre post post_err l) : EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l = frame2_repr a frame ppre pre p post post_err l inner () noeq [@erasable] // very important, otherwise KaRaMeL will fail with argument typing type valid_rewrite_t (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) = { valid_rewrite_valid: (h: HS.mem) -> (b: B.buffer U8.t) -> (pos: U32.t) -> (pos' : U32.t) -> Lemma (requires ( valid_pos p1 h b pos pos' /\ precond (contents p1 h b pos pos') )) (ensures ( valid_pos p1 h b pos pos' /\ ( let x = contents p1 h b pos pos' in precond x /\ valid_pos p2 h b pos pos' /\ contents p2 h b pos pos' == f x ))); valid_rewrite_size: (x: Parser?.t p1 { precond x }) -> Lemma (size p1 x == size p2 (f x)) } let valid_rewrite_implies (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (precond' : pre_t p1) (f' : (x: Parser?.t p1 { precond' x }) -> GTot (Parser?.t p2)) : Pure (valid_rewrite_t p1 p2 precond' f') (requires ( (forall (x: Parser?.t p1) . precond' x ==> precond x) /\ (forall (x: Parser?.t p1 { precond' x }) . f' x == f x) )) (ensures (fun _ -> True)) = { valid_rewrite_valid = (fun h b pos pos' -> v.valid_rewrite_valid h b pos pos'); valid_rewrite_size = (fun x -> v.valid_rewrite_size x); } let valid_rewrite_compose (p1: parser) (p2: parser) (precond12: pre_t p1) (f12: (x: Parser?.t p1 { precond12 x }) -> GTot (Parser?.t p2)) (v12: valid_rewrite_t p1 p2 precond12 f12) (p3: parser) (precond23: pre_t p2) (f23: (x: Parser?.t p2 { precond23 x }) -> GTot (Parser?.t p3)) (v23: valid_rewrite_t p2 p3 precond23 f23) : Tot (valid_rewrite_t p1 p3 (fun x -> precond12 x /\ precond23 (f12 x)) (fun x -> f23 (f12 x))) = { valid_rewrite_valid = (fun h b pos pos' -> v12.valid_rewrite_valid h b pos pos'; v23.valid_rewrite_valid h b pos pos' ); valid_rewrite_size = (fun x -> v12.valid_rewrite_size x; v23.valid_rewrite_size (f12 x) ); } let valid_rewrite_spec (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) : Tot (repr_spec unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False)) = fun vin -> v.valid_rewrite_size vin; Correct ((), f vin) inline_for_extraction val valid_rewrite_impl (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) : Tot (repr_impl unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False) inv (valid_rewrite_spec p1 p2 precond f v)) inline_for_extraction let valid_rewrite_repr (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) : Tot (repr unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False) inv) = Repr _ (valid_rewrite_impl p1 p2 precond f v inv) inline_for_extraction let valid_rewrite (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (inv: memory_invariant) (v: valid_rewrite_t p1 p2 precond f) : Write unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) inv = EWrite?.reflect (valid_rewrite_repr p1 p2 precond f v inv) inline_for_extraction val cast (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) (x1: ptr p1 inv { precond (deref_spec x1) }) : Tot (x2: ptr p2 inv { deref_spec x2 == f (deref_spec x1) }) let valid_rewrite_parse_pair_assoc_1 (p1 p2 p3: parser) : Tot (valid_rewrite_t ((p1 `parse_pair` p2) `parse_pair` p3) (p1 `parse_pair` (p2 `parse_pair` p3)) (fun _ -> True) (fun ((x1, x2), x3) -> (x1, (x2, x3)))) = { valid_rewrite_valid = (fun h b pos pos' -> let pos3 = valid_parse_pair_inv_spec h (p1 `parse_pair` p2) p3 b pos pos' in let pos2 = valid_parse_pair_inv_spec h p1 p2 b pos pos3 in valid_parse_pair p2 p3 h b pos2 pos3 pos'; valid_parse_pair p1 (p2 `parse_pair` p3) h b pos pos2 pos' ); valid_rewrite_size = (fun _ -> ()); } let valid_rewrite_parse_pair_assoc_2 (p1 p2 p3: parser) : Tot (valid_rewrite_t (p1 `parse_pair` (p2 `parse_pair` p3)) ((p1 `parse_pair` p2) `parse_pair` p3) (fun _ -> True) (fun (x1, (x2, x3)) -> ((x1, x2), x3))) = { valid_rewrite_valid = (fun h b pos pos' -> let pos2 = valid_parse_pair_inv_spec h p1 (p2 `parse_pair` p3) b pos pos' in let pos3 = valid_parse_pair_inv_spec h p2 p3 b pos2 pos' in valid_parse_pair p1 p2 h b pos pos2 pos3; valid_parse_pair (p1 `parse_pair` p2) p3 h b pos pos3 pos' ); valid_rewrite_size = (fun _ -> ()); } let valid_rewrite_parse_pair_compat_l (p: parser) (p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) ($v: valid_rewrite_t p1 p2 precond f) : Tot (valid_rewrite_t (p `parse_pair` p1) (p `parse_pair` p2) (fun (_, x) -> precond x) (fun (x, y) -> (x, f y))) = { valid_rewrite_valid = (fun h b pos pos' -> let posq = valid_parse_pair_inv_spec h p p1 b pos pos' in v.valid_rewrite_valid h b posq pos'; valid_parse_pair p p2 h b pos posq pos' ); valid_rewrite_size = (fun xy -> let (_, x) = (xy <: Parser?.t (p `parse_pair` p1)) in v.valid_rewrite_size x ); } let valid_rewrite_parse_pair_compat_r (p: parser) (p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) ($v: valid_rewrite_t p1 p2 precond f) : Tot (valid_rewrite_t (p1 `parse_pair` p) (p2 `parse_pair` p) (fun (x, _) -> precond x) (fun (x, y) -> (f x, y))) = { valid_rewrite_valid = (fun h b pos pos' -> let posp = valid_parse_pair_inv_spec h p1 p b pos pos' in v.valid_rewrite_valid h b pos posp; valid_parse_pair p2 p h b pos posp pos' ); valid_rewrite_size = (fun xy -> let (x, _) = (xy <: Parser?.t (p1 `parse_pair` p)) in v.valid_rewrite_size x ); } let check_precond_t (p1: parser) (precond: pre_t p1) : Tot Type = (b: B.buffer U8.t) -> (len: U32.t { B.len b == len }) -> (pos: U32.t) -> (pos' : U32.t) -> HST.Stack bool (requires (fun h -> valid_pos p1 h b pos pos' )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ (res == true <==> precond (contents p1 h b pos pos')) )) inline_for_extraction val check_precond_repr (p1: parser) (precond: pre_t p1) (c: check_precond_t p1 precond) (inv: memory_invariant) : Tot (repr unit p1 (p1) precond (fun vin _ vout -> vin == vout /\ precond vin) (fun vin -> ~ (precond vin)) inv) inline_for_extraction let check_precond (p1: parser) (precond: pre_t p1) (c: check_precond_t p1 precond) (inv: memory_invariant) : EWrite unit p1 (p1) precond (fun vin _ vout -> vin == vout /\ precond vin) (fun vin -> ~ (precond vin)) inv = EWrite?.reflect (check_precond_repr p1 precond c inv) let cat_spec (#inv: memory_invariant) (#p: parser) (x: ptr p inv) : Tot (repr_spec unit parse_empty p (fun _ -> True) (fun _ _ vout -> vout == deref_spec x) (fun _ -> False)) = fun _ -> Correct ((), deref_spec x) inline_for_extraction val cat_impl (#inv: memory_invariant) (#p: parser) (x: ptr p inv) : Tot (repr_impl _ _ _ _ _ _ inv (cat_spec x))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.ptr p inv -> LowParseWriters.Write Prims.unit
LowParseWriters.Write
[]
[]
[ "LowParseWriters.memory_invariant", "LowParseWriters.LowParse.parser", "LowParseWriters.ptr", "LowParseWriters.Repr", "Prims.unit", "LowParseWriters.LowParse.parse_empty", "LowParseWriters.LowParse.__proj__Parser__item__t", "Prims.l_True", "Prims.eq2", "LowParseWriters.deref_spec", "Prims.l_False", "LowParseWriters.cat_spec", "LowParseWriters.cat_impl" ]
[]
false
true
false
false
false
let cat (#inv: memory_invariant) (#p: parser) (x: ptr p inv) : Write unit parse_empty p (fun _ -> True) (fun _ _ vout -> vout == deref_spec x) inv =
EWrite?.reflect (Repr _ (cat_impl x))
false
LowParseWriters.fsti
LowParseWriters.frame
val frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: (unit -> EWrite a parse_empty p pre post post_err l)) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l
val frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: (unit -> EWrite a parse_empty p pre post post_err l)) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l
let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner ()
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 51, "end_line": 1508, "start_col": 0, "start_line": 1498 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> frame: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t LowParseWriters.LowParse.parse_empty -> p: LowParseWriters.LowParse.parser -> post: LowParseWriters.post_t a LowParseWriters.LowParse.parse_empty p pre -> post_err: LowParseWriters.post_err_t LowParseWriters.LowParse.parse_empty pre -> l: LowParseWriters.memory_invariant -> $inner: (_: Prims.unit -> LowParseWriters.EWrite a) -> LowParseWriters.EWrite a
LowParseWriters.EWrite
[]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.LowParse.parse_empty", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.memory_invariant", "Prims.unit", "LowParseWriters.frame_repr", "LowParseWriters.frame_out", "LowParseWriters.frame_pre", "LowParseWriters.frame_post", "LowParseWriters.frame_post_err" ]
[]
false
true
false
false
false
let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: (unit -> EWrite a parse_empty p pre post post_err l)) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l =
frame_repr a frame pre p post post_err l inner ()
false
Steel.Effect.Atomic.fst
Steel.Effect.Atomic.bind_opaque
val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True)
val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True)
let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_f x `star` frame_f) (core_mem m1) in let h1' = mk_rmem (pre_g x `star` frame_g x) (core_mem m1) in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); focus_is_restrict_mk_rmem (post_f x `star` frame_f) (pre_g x `star` frame_g x) (core_mem m1); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of (post_f x `star` frame_f)) (hp_of (pre_g x `star` frame_g x)) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); let h2' = mk_rmem (post_g x y `star` frame_g x) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in // assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem (post_g x y `star` frame_g x) (post y) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of (post_g x y `star` frame_g x)) (hp_of (post y)) frame (locks_invariant opened m2) m2; y
{ "file_name": "lib/steel/Steel.Effect.Atomic.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 5, "end_line": 271, "start_col": 0, "start_line": 196 }
(* 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.Atomic open Steel.Effect friend Steel.Effect #set-options "--warn_error -330" //turn off the experimental feature warning let _ : squash (forall (pre:pre_t) (m0:mem{interp (hp_of pre) m0}) (m1:mem{disjoint m0 m1}). mk_rmem pre m0 == mk_rmem pre (join m0 m1)) = Classical.forall_intro rmem_depends_only_on let req_to_act_req (#pre:vprop) (req:req_t pre) : mprop (hp_of pre) = fun m -> rmem_depends_only_on pre; interp (hp_of pre) m /\ req (mk_rmem pre m) unfold let to_post (#a:Type) (post:post_t a) = fun x -> (hp_of (post x)) let ens_to_act_ens (#pre:pre_t) (#a:Type) (#post:post_t a) (ens:ens_t pre a post) : mprop2 (hp_of pre) (to_post post) = fun m0 x m1 -> interp (hp_of pre) m0 /\ interp (hp_of (post x)) m1 /\ ens (mk_rmem pre m0) x (mk_rmem (post x) m1) let repr a framed opened f pre post req ens = action_except_full a opened (hp_of pre) (to_post post) (req_to_act_req req) (ens_to_act_ens ens) let return_ a x opened #p = fun _ -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (p x) (core_mem m0) in lemma_frame_equalities_refl (p x) h0; x #push-options "--fuel 0 --ifuel 0" #push-options "--z3rlimit 20 --fuel 1 --ifuel 1" val frame00 (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:repr a framed opened obs pre post req ens) (frame:vprop) : repr a true opened obs (pre `star` frame) (fun x -> post x `star` frame) (fun h -> req (focus_rmem h pre)) (fun h0 r h1 -> req (focus_rmem h0 pre) /\ ens (focus_rmem h0 pre) r (focus_rmem h1 (post r)) /\ frame_opaque frame (focus_rmem h0 frame) (focus_rmem h1 frame)) module Sem = Steel.Semantics.Hoare.MST module Mem = Steel.Memory let equiv_middle_left_assoc (a b c d:slprop) : Lemma (((a `Mem.star` b) `Mem.star` c `Mem.star` d) `Mem.equiv` (a `Mem.star` (b `Mem.star` c) `Mem.star` d)) = let open Steel.Memory in star_associative a b c; star_congruence ((a `star` b) `star` c) d (a `star` (b `star` c)) d let frame00 #a #framed #opened #obs #pre #post #req #ens f frame = fun frame' -> let m0:full_mem = NMSTTotal.get () in let snap:rmem frame = mk_rmem frame (core_mem m0) in // Need to define it with type annotation, although unused, for it to trigger // the pattern on the framed ensures in the def of MstTot let aux:mprop (hp_of frame `Mem.star` frame') = req_frame frame snap in focus_is_restrict_mk_rmem (pre `star` frame) pre (core_mem m0); assert (interp (hp_of (pre `star` frame) `Mem.star` frame' `Mem.star` locks_invariant opened m0) m0); equiv_middle_left_assoc (hp_of pre) (hp_of frame) frame' (locks_invariant opened m0); assert (interp (hp_of pre `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m0) m0); let x = f (hp_of frame `Mem.star` frame') in let m1:full_mem = NMSTTotal.get () in assert (interp (hp_of (post x) `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m1) m1); equiv_middle_left_assoc (hp_of (post x)) (hp_of frame) frame' (locks_invariant opened m1); assert (interp ((hp_of (post x) `Mem.star` hp_of frame) `Mem.star` frame' `Mem.star` locks_invariant opened m1) m1); focus_is_restrict_mk_rmem (pre `star` frame) frame (core_mem m0); focus_is_restrict_mk_rmem (post x `star` frame) frame (core_mem m1); let h0:rmem (pre `star` frame) = mk_rmem (pre `star` frame) (core_mem m0) in let h1:rmem (post x `star` frame) = mk_rmem (post x `star` frame) (core_mem m1) in assert (focus_rmem h0 frame == focus_rmem h1 frame); focus_is_restrict_mk_rmem (post x `star` frame) (post x) (core_mem m1); lemma_frame_opaque_refl frame (focus_rmem h0 frame); x unfold let bind_req_opaque (#a:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#pr:a -> prop) (req_g:(x:a -> req_t (pre_g x))) (frame_f:vprop) (frame_g:a -> vprop) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) : req_t (pre_f `star` frame_f) = fun m0 -> req_f (focus_rmem m0 pre_f) /\ (forall (x:a) (h1:hmem (post_f x `star` frame_f)). (ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f)) ==> pr x /\ (can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); (req_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)))) unfold let bind_ens_opaque (#a:Type) (#b:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#pr:a -> prop) (ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (frame_f:vprop) (frame_g:a -> vprop) (post:post_t b) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (_:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) : ens_t (pre_f `star` frame_f) b post = fun m0 y m2 -> req_f (focus_rmem m0 pre_f) /\ (exists (x:a) (h1:hmem (post_f x `star` frame_f)). pr x /\ ( can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f) /\ frame_opaque (frame_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (frame_g x)) (focus_rmem m2 (frame_g x)) /\ ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ (ens_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)) y (focus_rmem m2 (post_g x y)))) val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True)
{ "checked_file": "/", "dependencies": [ "Steel.Semantics.Hoare.MST.fst.checked", "Steel.Memory.fsti.checked", "Steel.Effect.fst.checked", "Steel.Effect.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.NMSTTotal.fst.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "Steel.Effect.Atomic.fst" }
[ { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "Steel.Semantics.Hoare.MST", "short_module": "Sem" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Common", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics", "short_module": "T" }, { "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": 1, "initial_ifuel": 1, "max_fuel": 1, "max_ifuel": 1, "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": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Type -> b: Type -> opened_invariants: Steel.Memory.inames -> o1: FStar.Pervasives.eqtype_as_type Steel.Effect.Common.observability -> o2: FStar.Pervasives.eqtype_as_type Steel.Effect.Common.observability -> f: Steel.Effect.Atomic.repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f -> g: (x: a -> Steel.Effect.Atomic.repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x)) -> Prims.Pure (Steel.Effect.Atomic.repr b true opened_invariants (Steel.Effect.Common.join_obs o1 o2) (Steel.Effect.Common.star pre_f frame_f) post (Steel.Effect.Atomic.bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (Steel.Effect.Atomic.bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2))
Prims.Pure
[]
[]
[ "Steel.Memory.inames", "FStar.Pervasives.eqtype_as_type", "Steel.Effect.Common.observability", "Prims.bool", "Steel.Effect.Common.pre_t", "Steel.Effect.Common.post_t", "Steel.Effect.Common.req_t", "Steel.Effect.Common.ens_t", "Steel.Effect.Common.vprop", "Prims.squash", "Steel.Effect.Common.maybe_emp", "Steel.Effect.Common.maybe_emp_dep", "Prims.prop", "Steel.Effect.Common.can_be_split_forall_dep", "Steel.Effect.Common.star", "Steel.Effect.Common.can_be_split_post", "Steel.Effect.Atomic.repr", "Steel.Memory.slprop", "Prims.unit", "Steel.Effect.can_be_split_3_interp", "Steel.Effect.Common.hp_of", "Steel.Memory.locks_invariant", "Steel.Effect.focus_focus_is_focus", "Steel.Memory.core_mem", "Steel.Effect.focus_is_restrict_mk_rmem", "Steel.Effect.Common.rmem'", "Steel.Effect.Common.valid_rmem", "Steel.Effect.Common.mk_rmem", "Steel.Effect.Common.can_be_split_trans", "Steel.Memory.full_mem", "FStar.NMSTTotal.get", "Steel.Memory.mem_evolves", "Steel.Effect.Atomic.frame00", "Prims._assert", "Prims.eq2", "Steel.Effect.Common.rmem", "Steel.Effect.Common.focus_rmem" ]
[]
false
false
false
false
false
let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g =
fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem ((post_f x) `star` frame_f) (core_mem m1) in let h1' = mk_rmem ((pre_g x) `star` (frame_g x)) (core_mem m1) in can_be_split_trans ((post_f x) `star` frame_f) ((pre_g x) `star` (frame_g x)) (pre_g x); focus_is_restrict_mk_rmem ((post_f x) `star` frame_f) ((pre_g x) `star` (frame_g x)) (core_mem m1); focus_focus_is_focus ((post_f x) `star` frame_f) ((pre_g x) `star` (frame_g x)) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of ((post_f x) `star` frame_f)) (hp_of ((pre_g x) `star` (frame_g x))) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans ((post_f x) `star` frame_f) ((pre_g x) `star` (frame_g x)) (pre_g x); can_be_split_trans ((post_f x) `star` frame_f) ((pre_g x) `star` (frame_g x)) (frame_g x); can_be_split_trans (post y) ((post_g x y) `star` (frame_g x)) (post_g x y); can_be_split_trans (post y) ((post_g x y) `star` (frame_g x)) (frame_g x); let h2' = mk_rmem ((post_g x y) `star` (frame_g x)) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in focus_focus_is_focus ((post_f x) `star` frame_f) ((pre_g x) `star` (frame_g x)) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem ((post_g x y) `star` (frame_g x)) (post y) (core_mem m2); focus_focus_is_focus ((post_g x y) `star` (frame_g x)) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus ((post_g x y) `star` (frame_g x)) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of ((post_g x y) `star` (frame_g x))) (hp_of (post y)) frame (locks_invariant opened m2) m2; y
false
LowParseWriters.fsti
LowParseWriters.frame2_spec
val frame2_spec (a: Type) (frame ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: (unit -> EWrite a ppre p pre post post_err l)) : Tot (repr_spec a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err))
val frame2_spec (a: Type) (frame ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: (unit -> EWrite a ppre p pre post post_err l)) : Tot (repr_spec a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err))
let frame2_spec (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_spec a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err)) = fun (fr, w_in) -> match destr_repr_spec a ppre p pre post post_err l inner w_in with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 22, "end_line": 1697, "start_col": 0, "start_line": 1683 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in inline_for_extraction val recast_writer_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f)) inline_for_extraction let recast_writer_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) = mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f) inline_for_extraction let recast_writer (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: unit -> EWrite a r_in r_out pre post post_err l) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l = recast_writer_repr a r_in r_out pre post post_err l f () inline_for_extraction let frame' (a: Type) (fr: parser) (p: parser) (l: memory_invariant) (f: unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l ) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout) ) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ()) ) l = frame _ _ _ _ _ _ _ (fun _ -> recast_writer _ _ _ _ _ _ _ f) unfold let frame2_pre (frame: parser) (ppre: parser) (pre: pre_t ppre) : Tot (pre_t (frame `parse_pair` ppre)) = fun (_, x) -> pre x unfold let frame2_post (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) : Tot (post_t a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre)) = fun (v_frame, v_in) v (v_frame', v_out) -> v_frame == v_frame' /\ post v_in v v_out unfold let frame2_post_err (frame: parser) (ppre: parser) (pre: pre_t ppre) (post_err: post_err_t ppre pre) : Tot (post_err_t (frame `parse_pair` ppre) (frame2_pre frame ppre pre)) = fun (_, x) -> post_err x
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> frame: LowParseWriters.LowParse.parser -> ppre: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t ppre -> p: LowParseWriters.LowParse.parser -> post: LowParseWriters.post_t a ppre p pre -> post_err: LowParseWriters.post_err_t ppre pre -> l: LowParseWriters.memory_invariant -> inner: (_: Prims.unit -> LowParseWriters.EWrite a) -> LowParseWriters.repr_spec a (LowParseWriters.LowParse.parse_pair frame ppre) (LowParseWriters.frame_out a frame p) (LowParseWriters.frame2_pre frame ppre pre) (LowParseWriters.frame2_post a frame ppre pre p post) (LowParseWriters.frame2_post_err frame ppre pre post_err)
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.memory_invariant", "Prims.unit", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.LowParse.parse_pair", "LowParseWriters.destr_repr_spec", "LowParseWriters.Correct", "FStar.Pervasives.Native.tuple2", "LowParseWriters.frame_out", "FStar.Pervasives.Native.Mktuple2", "Prims.string", "LowParseWriters.Error", "LowParseWriters.result", "LowParseWriters.repr_spec", "LowParseWriters.frame2_pre", "LowParseWriters.frame2_post", "LowParseWriters.frame2_post_err" ]
[]
false
false
false
false
false
let frame2_spec (a: Type) (frame ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: (unit -> EWrite a ppre p pre post post_err l)) : Tot (repr_spec a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err)) =
fun (fr, w_in) -> match destr_repr_spec a ppre p pre post post_err l inner w_in with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e
false
Pulse.C.Types.UserStruct.fsti
Pulse.C.Types.UserStruct.set_snoc
val set_snoc (#t: eqtype) (q: FStar.Set.set t) (a: t) : Pure (nonempty_set t) (requires True) (ensures (fun s -> (forall (x: t). {:pattern FStar.Set.mem x s} FStar.Set.mem x s == (x = a || FStar.Set.mem x q))))
val set_snoc (#t: eqtype) (q: FStar.Set.set t) (a: t) : Pure (nonempty_set t) (requires True) (ensures (fun s -> (forall (x: t). {:pattern FStar.Set.mem x s} FStar.Set.mem x s == (x = a || FStar.Set.mem x q))))
let set_snoc // for associativity reasons (#t: eqtype) (q: FStar.Set.set t) (a: t) : Pure (nonempty_set t) (requires True) (ensures (fun s -> (forall (x: t). {:pattern FStar.Set.mem x s} FStar.Set.mem x s == (x = a || FStar.Set.mem x q)) )) = q `FStar.Set.union` FStar.Set.singleton a
{ "file_name": "share/steel/examples/pulse/lib/c/Pulse.C.Types.UserStruct.fsti", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 43, "end_line": 29, "start_col": 0, "start_line": 23 }
module Pulse.C.Types.UserStruct open Pulse.Lib.Pervasives open Pulse.C.Types.Struct.Aux module Set = FStar.Set (* This library allows the user to define their own struct type, with a constructor from field values, and a destructor to field values for each field. This may be necessary for recursive structs *) let set_def (#t: eqtype) (s: FStar.Set.set t) (x: t) : Tot bool = FStar.Set.mem x s noextract let nonempty_set (t: eqtype) = (s: Set.set t { exists x . set_def s x == true })
{ "checked_file": "/", "dependencies": [ "Pulse.Lib.Pervasives.fst.checked", "Pulse.C.Types.Struct.Aux.fsti.checked", "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Pulse.C.Types.UserStruct.fsti" }
[ { "abbrev": true, "full_module": "FStar.Set", "short_module": "Set" }, { "abbrev": false, "full_module": "Pulse.C.Types.Struct.Aux", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Lib.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Pulse.C.Types", "short_module": null }, { "abbrev": false, "full_module": "Pulse.C.Types", "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
q: FStar.Set.set t -> a: t -> Prims.Pure (Pulse.C.Types.UserStruct.nonempty_set t)
Prims.Pure
[]
[]
[ "Prims.eqtype", "FStar.Set.set", "FStar.Set.union", "FStar.Set.singleton", "Pulse.C.Types.UserStruct.nonempty_set", "Prims.l_True", "Prims.l_Forall", "Prims.eq2", "Prims.bool", "FStar.Set.mem", "Prims.op_BarBar", "Prims.op_Equality" ]
[]
false
false
false
false
false
let set_snoc (#t: eqtype) (q: FStar.Set.set t) (a: t) : Pure (nonempty_set t) (requires True) (ensures (fun s -> (forall (x: t). {:pattern FStar.Set.mem x s} FStar.Set.mem x s == (x = a || FStar.Set.mem x q)))) =
q `FStar.Set.union` (FStar.Set.singleton a)
false
LowParseWriters.fsti
LowParseWriters.valid_rewrite_compose
val valid_rewrite_compose (p1 p2: parser) (precond12: pre_t p1) (f12: (x: Parser?.t p1 {precond12 x} -> GTot (Parser?.t p2))) (v12: valid_rewrite_t p1 p2 precond12 f12) (p3: parser) (precond23: pre_t p2) (f23: (x: Parser?.t p2 {precond23 x} -> GTot (Parser?.t p3))) (v23: valid_rewrite_t p2 p3 precond23 f23) : Tot (valid_rewrite_t p1 p3 (fun x -> precond12 x /\ precond23 (f12 x)) (fun x -> f23 (f12 x)))
val valid_rewrite_compose (p1 p2: parser) (precond12: pre_t p1) (f12: (x: Parser?.t p1 {precond12 x} -> GTot (Parser?.t p2))) (v12: valid_rewrite_t p1 p2 precond12 f12) (p3: parser) (precond23: pre_t p2) (f23: (x: Parser?.t p2 {precond23 x} -> GTot (Parser?.t p3))) (v23: valid_rewrite_t p2 p3 precond23 f23) : Tot (valid_rewrite_t p1 p3 (fun x -> precond12 x /\ precond23 (f12 x)) (fun x -> f23 (f12 x)))
let valid_rewrite_compose (p1: parser) (p2: parser) (precond12: pre_t p1) (f12: (x: Parser?.t p1 { precond12 x }) -> GTot (Parser?.t p2)) (v12: valid_rewrite_t p1 p2 precond12 f12) (p3: parser) (precond23: pre_t p2) (f23: (x: Parser?.t p2 { precond23 x }) -> GTot (Parser?.t p3)) (v23: valid_rewrite_t p2 p3 precond23 f23) : Tot (valid_rewrite_t p1 p3 (fun x -> precond12 x /\ precond23 (f12 x)) (fun x -> f23 (f12 x))) = { valid_rewrite_valid = (fun h b pos pos' -> v12.valid_rewrite_valid h b pos pos'; v23.valid_rewrite_valid h b pos pos' ); valid_rewrite_size = (fun x -> v12.valid_rewrite_size x; v23.valid_rewrite_size (f12 x) ); }
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 1, "end_line": 1813, "start_col": 0, "start_line": 1793 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in inline_for_extraction val recast_writer_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f)) inline_for_extraction let recast_writer_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) = mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f) inline_for_extraction let recast_writer (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: unit -> EWrite a r_in r_out pre post post_err l) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l = recast_writer_repr a r_in r_out pre post post_err l f () inline_for_extraction let frame' (a: Type) (fr: parser) (p: parser) (l: memory_invariant) (f: unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l ) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout) ) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ()) ) l = frame _ _ _ _ _ _ _ (fun _ -> recast_writer _ _ _ _ _ _ _ f) unfold let frame2_pre (frame: parser) (ppre: parser) (pre: pre_t ppre) : Tot (pre_t (frame `parse_pair` ppre)) = fun (_, x) -> pre x unfold let frame2_post (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) : Tot (post_t a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre)) = fun (v_frame, v_in) v (v_frame', v_out) -> v_frame == v_frame' /\ post v_in v v_out unfold let frame2_post_err (frame: parser) (ppre: parser) (pre: pre_t ppre) (post_err: post_err_t ppre pre) : Tot (post_err_t (frame `parse_pair` ppre) (frame2_pre frame ppre pre)) = fun (_, x) -> post_err x let frame2_spec (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_spec a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err)) = fun (fr, w_in) -> match destr_repr_spec a ppre p pre post post_err l inner w_in with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame2_impl (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_impl a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner)) inline_for_extraction let frame2_repr (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (unit -> EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l) = mk_repr a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner) (frame2_impl a frame ppre pre p post post_err l inner) inline_for_extraction let frame2 (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) ($inner: unit -> EWrite a ppre p pre post post_err l) : EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l = frame2_repr a frame ppre pre p post post_err l inner () noeq [@erasable] // very important, otherwise KaRaMeL will fail with argument typing type valid_rewrite_t (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) = { valid_rewrite_valid: (h: HS.mem) -> (b: B.buffer U8.t) -> (pos: U32.t) -> (pos' : U32.t) -> Lemma (requires ( valid_pos p1 h b pos pos' /\ precond (contents p1 h b pos pos') )) (ensures ( valid_pos p1 h b pos pos' /\ ( let x = contents p1 h b pos pos' in precond x /\ valid_pos p2 h b pos pos' /\ contents p2 h b pos pos' == f x ))); valid_rewrite_size: (x: Parser?.t p1 { precond x }) -> Lemma (size p1 x == size p2 (f x)) } let valid_rewrite_implies (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (precond' : pre_t p1) (f' : (x: Parser?.t p1 { precond' x }) -> GTot (Parser?.t p2)) : Pure (valid_rewrite_t p1 p2 precond' f') (requires ( (forall (x: Parser?.t p1) . precond' x ==> precond x) /\ (forall (x: Parser?.t p1 { precond' x }) . f' x == f x) )) (ensures (fun _ -> True)) = { valid_rewrite_valid = (fun h b pos pos' -> v.valid_rewrite_valid h b pos pos'); valid_rewrite_size = (fun x -> v.valid_rewrite_size x); }
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.LowParse.parser -> p2: LowParseWriters.LowParse.parser -> precond12: LowParseWriters.pre_t p1 -> f12: (x: Parser?.t p1 {precond12 x} -> Prims.GTot (Parser?.t p2)) -> v12: LowParseWriters.valid_rewrite_t p1 p2 precond12 f12 -> p3: LowParseWriters.LowParse.parser -> precond23: LowParseWriters.pre_t p2 -> f23: (x: Parser?.t p2 {precond23 x} -> Prims.GTot (Parser?.t p3)) -> v23: LowParseWriters.valid_rewrite_t p2 p3 precond23 f23 -> LowParseWriters.valid_rewrite_t p1 p3 (fun x -> precond12 x /\ precond23 (f12 x)) (fun x -> f23 (f12 x))
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.valid_rewrite_t", "LowParseWriters.Mkvalid_rewrite_t", "Prims.l_and", "FStar.Monotonic.HyperStack.mem", "LowStar.Buffer.buffer", "FStar.UInt8.t", "FStar.UInt32.t", "LowParseWriters.__proj__Mkvalid_rewrite_t__item__valid_rewrite_valid", "Prims.unit", "LowParseWriters.__proj__Mkvalid_rewrite_t__item__valid_rewrite_size" ]
[]
false
false
false
false
false
let valid_rewrite_compose (p1 p2: parser) (precond12: pre_t p1) (f12: (x: Parser?.t p1 {precond12 x} -> GTot (Parser?.t p2))) (v12: valid_rewrite_t p1 p2 precond12 f12) (p3: parser) (precond23: pre_t p2) (f23: (x: Parser?.t p2 {precond23 x} -> GTot (Parser?.t p3))) (v23: valid_rewrite_t p2 p3 precond23 f23) : Tot (valid_rewrite_t p1 p3 (fun x -> precond12 x /\ precond23 (f12 x)) (fun x -> f23 (f12 x))) =
{ valid_rewrite_valid = (fun h b pos pos' -> v12.valid_rewrite_valid h b pos pos'; v23.valid_rewrite_valid h b pos pos'); valid_rewrite_size = (fun x -> v12.valid_rewrite_size x; v23.valid_rewrite_size (f12 x)) }
false
LowParseWriters.fsti
LowParseWriters.valid_rewrite
val valid_rewrite (p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 {precond x} -> GTot (Parser?.t p2))) (inv: memory_invariant) (v: valid_rewrite_t p1 p2 precond f) : Write unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) inv
val valid_rewrite (p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 {precond x} -> GTot (Parser?.t p2))) (inv: memory_invariant) (v: valid_rewrite_t p1 p2 precond f) : Write unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) inv
let valid_rewrite (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (inv: memory_invariant) (v: valid_rewrite_t p1 p2 precond f) : Write unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) inv = EWrite?.reflect (valid_rewrite_repr p1 p2 precond f v inv)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 60, "end_line": 1856, "start_col": 0, "start_line": 1848 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in inline_for_extraction val recast_writer_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f)) inline_for_extraction let recast_writer_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) = mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f) inline_for_extraction let recast_writer (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: unit -> EWrite a r_in r_out pre post post_err l) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l = recast_writer_repr a r_in r_out pre post post_err l f () inline_for_extraction let frame' (a: Type) (fr: parser) (p: parser) (l: memory_invariant) (f: unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l ) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout) ) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ()) ) l = frame _ _ _ _ _ _ _ (fun _ -> recast_writer _ _ _ _ _ _ _ f) unfold let frame2_pre (frame: parser) (ppre: parser) (pre: pre_t ppre) : Tot (pre_t (frame `parse_pair` ppre)) = fun (_, x) -> pre x unfold let frame2_post (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) : Tot (post_t a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre)) = fun (v_frame, v_in) v (v_frame', v_out) -> v_frame == v_frame' /\ post v_in v v_out unfold let frame2_post_err (frame: parser) (ppre: parser) (pre: pre_t ppre) (post_err: post_err_t ppre pre) : Tot (post_err_t (frame `parse_pair` ppre) (frame2_pre frame ppre pre)) = fun (_, x) -> post_err x let frame2_spec (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_spec a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err)) = fun (fr, w_in) -> match destr_repr_spec a ppre p pre post post_err l inner w_in with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame2_impl (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_impl a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner)) inline_for_extraction let frame2_repr (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (unit -> EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l) = mk_repr a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner) (frame2_impl a frame ppre pre p post post_err l inner) inline_for_extraction let frame2 (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) ($inner: unit -> EWrite a ppre p pre post post_err l) : EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l = frame2_repr a frame ppre pre p post post_err l inner () noeq [@erasable] // very important, otherwise KaRaMeL will fail with argument typing type valid_rewrite_t (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) = { valid_rewrite_valid: (h: HS.mem) -> (b: B.buffer U8.t) -> (pos: U32.t) -> (pos' : U32.t) -> Lemma (requires ( valid_pos p1 h b pos pos' /\ precond (contents p1 h b pos pos') )) (ensures ( valid_pos p1 h b pos pos' /\ ( let x = contents p1 h b pos pos' in precond x /\ valid_pos p2 h b pos pos' /\ contents p2 h b pos pos' == f x ))); valid_rewrite_size: (x: Parser?.t p1 { precond x }) -> Lemma (size p1 x == size p2 (f x)) } let valid_rewrite_implies (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (precond' : pre_t p1) (f' : (x: Parser?.t p1 { precond' x }) -> GTot (Parser?.t p2)) : Pure (valid_rewrite_t p1 p2 precond' f') (requires ( (forall (x: Parser?.t p1) . precond' x ==> precond x) /\ (forall (x: Parser?.t p1 { precond' x }) . f' x == f x) )) (ensures (fun _ -> True)) = { valid_rewrite_valid = (fun h b pos pos' -> v.valid_rewrite_valid h b pos pos'); valid_rewrite_size = (fun x -> v.valid_rewrite_size x); } let valid_rewrite_compose (p1: parser) (p2: parser) (precond12: pre_t p1) (f12: (x: Parser?.t p1 { precond12 x }) -> GTot (Parser?.t p2)) (v12: valid_rewrite_t p1 p2 precond12 f12) (p3: parser) (precond23: pre_t p2) (f23: (x: Parser?.t p2 { precond23 x }) -> GTot (Parser?.t p3)) (v23: valid_rewrite_t p2 p3 precond23 f23) : Tot (valid_rewrite_t p1 p3 (fun x -> precond12 x /\ precond23 (f12 x)) (fun x -> f23 (f12 x))) = { valid_rewrite_valid = (fun h b pos pos' -> v12.valid_rewrite_valid h b pos pos'; v23.valid_rewrite_valid h b pos pos' ); valid_rewrite_size = (fun x -> v12.valid_rewrite_size x; v23.valid_rewrite_size (f12 x) ); } let valid_rewrite_spec (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) : Tot (repr_spec unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False)) = fun vin -> v.valid_rewrite_size vin; Correct ((), f vin) inline_for_extraction val valid_rewrite_impl (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) : Tot (repr_impl unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False) inv (valid_rewrite_spec p1 p2 precond f v)) inline_for_extraction let valid_rewrite_repr (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) : Tot (repr unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False) inv) = Repr _ (valid_rewrite_impl p1 p2 precond f v inv)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.LowParse.parser -> p2: LowParseWriters.LowParse.parser -> precond: LowParseWriters.pre_t p1 -> f: (x: Parser?.t p1 {precond x} -> Prims.GTot (Parser?.t p2)) -> inv: LowParseWriters.memory_invariant -> v: LowParseWriters.valid_rewrite_t p1 p2 precond f -> LowParseWriters.Write Prims.unit
LowParseWriters.Write
[]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.memory_invariant", "LowParseWriters.valid_rewrite_t", "LowParseWriters.valid_rewrite_repr", "Prims.unit", "Prims.l_and", "Prims.eq2" ]
[]
false
true
false
false
false
let valid_rewrite (p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 {precond x} -> GTot (Parser?.t p2))) (inv: memory_invariant) (v: valid_rewrite_t p1 p2 precond f) : Write unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) inv =
EWrite?.reflect (valid_rewrite_repr p1 p2 precond f v inv)
false
Pulse.C.Types.UserStruct.fsti
Pulse.C.Types.UserStruct.nonempty_set_nonempty_type
val nonempty_set_nonempty_type (x: string) (s: Set.set string) : Lemma (requires (x `Set.mem` s)) (ensures (exists (x: field_t s). True))
val nonempty_set_nonempty_type (x: string) (s: Set.set string) : Lemma (requires (x `Set.mem` s)) (ensures (exists (x: field_t s). True))
let nonempty_set_nonempty_type (x: string) (s: Set.set string) : Lemma (requires (x `Set.mem` s)) (ensures (exists (x: field_t s) . True)) = Classical.exists_intro (fun (_: field_t s) -> True) x
{ "file_name": "share/steel/examples/pulse/lib/c/Pulse.C.Types.UserStruct.fsti", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 55, "end_line": 51, "start_col": 0, "start_line": 48 }
module Pulse.C.Types.UserStruct open Pulse.Lib.Pervasives open Pulse.C.Types.Struct.Aux module Set = FStar.Set (* This library allows the user to define their own struct type, with a constructor from field values, and a destructor to field values for each field. This may be necessary for recursive structs *) let set_def (#t: eqtype) (s: FStar.Set.set t) (x: t) : Tot bool = FStar.Set.mem x s noextract let nonempty_set (t: eqtype) = (s: Set.set t { exists x . set_def s x == true }) noextract let set_snoc // for associativity reasons (#t: eqtype) (q: FStar.Set.set t) (a: t) : Pure (nonempty_set t) (requires True) (ensures (fun s -> (forall (x: t). {:pattern FStar.Set.mem x s} FStar.Set.mem x s == (x = a || FStar.Set.mem x q)) )) = q `FStar.Set.union` FStar.Set.singleton a [@@noextract_to "krml"] let field_t (s: Set.set string) : Tot eqtype = (f: string { Set.mem f s }) [@@noextract_to "krml"; norm_field_attr] inline_for_extraction // for field_desc.fd_type noeq type struct_def (t: Type) = { fields: Set.set string; field_desc: field_description_gen_t (field_t fields); mk: ((f: field_t fields) -> Tot (field_desc.fd_type f)) -> Tot t; get: (t -> (f: field_t fields) -> Tot (field_desc.fd_type f)); get_mk: (phi: ((f: field_t fields) -> Tot (field_desc.fd_type f))) -> (f: field_t fields) -> Lemma (get (mk phi) f == phi f); extensionality: (x1: t) -> (x2: t) -> ((f: field_t fields) -> Lemma (get x1 f == get x2 f)) -> Lemma (x1 == x2); }
{ "checked_file": "/", "dependencies": [ "Pulse.Lib.Pervasives.fst.checked", "Pulse.C.Types.Struct.Aux.fsti.checked", "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Pulse.C.Types.UserStruct.fsti" }
[ { "abbrev": true, "full_module": "FStar.Set", "short_module": "Set" }, { "abbrev": false, "full_module": "Pulse.C.Types.Struct.Aux", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Lib.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Pulse.C.Types", "short_module": null }, { "abbrev": false, "full_module": "Pulse.C.Types", "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: Prims.string -> s: FStar.Set.set Prims.string -> FStar.Pervasives.Lemma (requires FStar.Set.mem x s) (ensures exists (x: Pulse.C.Types.UserStruct.field_t s). Prims.l_True)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Prims.string", "FStar.Set.set", "FStar.Classical.exists_intro", "Pulse.C.Types.UserStruct.field_t", "Prims.l_True", "Prims.unit", "Prims.b2t", "FStar.Set.mem", "Prims.squash", "Prims.l_Exists", "Prims.Nil", "FStar.Pervasives.pattern" ]
[]
false
false
true
false
false
let nonempty_set_nonempty_type (x: string) (s: Set.set string) : Lemma (requires (x `Set.mem` s)) (ensures (exists (x: field_t s). True)) =
Classical.exists_intro (fun (_: field_t s) -> True) x
false
LowParseWriters.fsti
LowParseWriters.valid_rewrite_parse_pair_compat_r
val valid_rewrite_parse_pair_compat_r (p p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 {precond x} -> GTot (Parser?.t p2))) ($v: valid_rewrite_t p1 p2 precond f) : Tot (valid_rewrite_t (p1 `parse_pair` p) (p2 `parse_pair` p) (fun (x, _) -> precond x) (fun (x, y) -> (f x, y)))
val valid_rewrite_parse_pair_compat_r (p p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 {precond x} -> GTot (Parser?.t p2))) ($v: valid_rewrite_t p1 p2 precond f) : Tot (valid_rewrite_t (p1 `parse_pair` p) (p2 `parse_pair` p) (fun (x, _) -> precond x) (fun (x, y) -> (f x, y)))
let valid_rewrite_parse_pair_compat_r (p: parser) (p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) ($v: valid_rewrite_t p1 p2 precond f) : Tot (valid_rewrite_t (p1 `parse_pair` p) (p2 `parse_pair` p) (fun (x, _) -> precond x) (fun (x, y) -> (f x, y))) = { valid_rewrite_valid = (fun h b pos pos' -> let posp = valid_parse_pair_inv_spec h p1 p b pos pos' in v.valid_rewrite_valid h b pos posp; valid_parse_pair p2 p h b pos posp pos' ); valid_rewrite_size = (fun xy -> let (x, _) = (xy <: Parser?.t (p1 `parse_pair` p)) in v.valid_rewrite_size x ); }
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 1, "end_line": 1933, "start_col": 0, "start_line": 1916 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in inline_for_extraction val recast_writer_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f)) inline_for_extraction let recast_writer_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) = mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f) inline_for_extraction let recast_writer (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: unit -> EWrite a r_in r_out pre post post_err l) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l = recast_writer_repr a r_in r_out pre post post_err l f () inline_for_extraction let frame' (a: Type) (fr: parser) (p: parser) (l: memory_invariant) (f: unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l ) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout) ) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ()) ) l = frame _ _ _ _ _ _ _ (fun _ -> recast_writer _ _ _ _ _ _ _ f) unfold let frame2_pre (frame: parser) (ppre: parser) (pre: pre_t ppre) : Tot (pre_t (frame `parse_pair` ppre)) = fun (_, x) -> pre x unfold let frame2_post (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) : Tot (post_t a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre)) = fun (v_frame, v_in) v (v_frame', v_out) -> v_frame == v_frame' /\ post v_in v v_out unfold let frame2_post_err (frame: parser) (ppre: parser) (pre: pre_t ppre) (post_err: post_err_t ppre pre) : Tot (post_err_t (frame `parse_pair` ppre) (frame2_pre frame ppre pre)) = fun (_, x) -> post_err x let frame2_spec (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_spec a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err)) = fun (fr, w_in) -> match destr_repr_spec a ppre p pre post post_err l inner w_in with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame2_impl (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_impl a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner)) inline_for_extraction let frame2_repr (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (unit -> EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l) = mk_repr a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner) (frame2_impl a frame ppre pre p post post_err l inner) inline_for_extraction let frame2 (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) ($inner: unit -> EWrite a ppre p pre post post_err l) : EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l = frame2_repr a frame ppre pre p post post_err l inner () noeq [@erasable] // very important, otherwise KaRaMeL will fail with argument typing type valid_rewrite_t (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) = { valid_rewrite_valid: (h: HS.mem) -> (b: B.buffer U8.t) -> (pos: U32.t) -> (pos' : U32.t) -> Lemma (requires ( valid_pos p1 h b pos pos' /\ precond (contents p1 h b pos pos') )) (ensures ( valid_pos p1 h b pos pos' /\ ( let x = contents p1 h b pos pos' in precond x /\ valid_pos p2 h b pos pos' /\ contents p2 h b pos pos' == f x ))); valid_rewrite_size: (x: Parser?.t p1 { precond x }) -> Lemma (size p1 x == size p2 (f x)) } let valid_rewrite_implies (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (precond' : pre_t p1) (f' : (x: Parser?.t p1 { precond' x }) -> GTot (Parser?.t p2)) : Pure (valid_rewrite_t p1 p2 precond' f') (requires ( (forall (x: Parser?.t p1) . precond' x ==> precond x) /\ (forall (x: Parser?.t p1 { precond' x }) . f' x == f x) )) (ensures (fun _ -> True)) = { valid_rewrite_valid = (fun h b pos pos' -> v.valid_rewrite_valid h b pos pos'); valid_rewrite_size = (fun x -> v.valid_rewrite_size x); } let valid_rewrite_compose (p1: parser) (p2: parser) (precond12: pre_t p1) (f12: (x: Parser?.t p1 { precond12 x }) -> GTot (Parser?.t p2)) (v12: valid_rewrite_t p1 p2 precond12 f12) (p3: parser) (precond23: pre_t p2) (f23: (x: Parser?.t p2 { precond23 x }) -> GTot (Parser?.t p3)) (v23: valid_rewrite_t p2 p3 precond23 f23) : Tot (valid_rewrite_t p1 p3 (fun x -> precond12 x /\ precond23 (f12 x)) (fun x -> f23 (f12 x))) = { valid_rewrite_valid = (fun h b pos pos' -> v12.valid_rewrite_valid h b pos pos'; v23.valid_rewrite_valid h b pos pos' ); valid_rewrite_size = (fun x -> v12.valid_rewrite_size x; v23.valid_rewrite_size (f12 x) ); } let valid_rewrite_spec (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) : Tot (repr_spec unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False)) = fun vin -> v.valid_rewrite_size vin; Correct ((), f vin) inline_for_extraction val valid_rewrite_impl (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) : Tot (repr_impl unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False) inv (valid_rewrite_spec p1 p2 precond f v)) inline_for_extraction let valid_rewrite_repr (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) : Tot (repr unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False) inv) = Repr _ (valid_rewrite_impl p1 p2 precond f v inv) inline_for_extraction let valid_rewrite (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (inv: memory_invariant) (v: valid_rewrite_t p1 p2 precond f) : Write unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) inv = EWrite?.reflect (valid_rewrite_repr p1 p2 precond f v inv) inline_for_extraction val cast (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) (x1: ptr p1 inv { precond (deref_spec x1) }) : Tot (x2: ptr p2 inv { deref_spec x2 == f (deref_spec x1) }) let valid_rewrite_parse_pair_assoc_1 (p1 p2 p3: parser) : Tot (valid_rewrite_t ((p1 `parse_pair` p2) `parse_pair` p3) (p1 `parse_pair` (p2 `parse_pair` p3)) (fun _ -> True) (fun ((x1, x2), x3) -> (x1, (x2, x3)))) = { valid_rewrite_valid = (fun h b pos pos' -> let pos3 = valid_parse_pair_inv_spec h (p1 `parse_pair` p2) p3 b pos pos' in let pos2 = valid_parse_pair_inv_spec h p1 p2 b pos pos3 in valid_parse_pair p2 p3 h b pos2 pos3 pos'; valid_parse_pair p1 (p2 `parse_pair` p3) h b pos pos2 pos' ); valid_rewrite_size = (fun _ -> ()); } let valid_rewrite_parse_pair_assoc_2 (p1 p2 p3: parser) : Tot (valid_rewrite_t (p1 `parse_pair` (p2 `parse_pair` p3)) ((p1 `parse_pair` p2) `parse_pair` p3) (fun _ -> True) (fun (x1, (x2, x3)) -> ((x1, x2), x3))) = { valid_rewrite_valid = (fun h b pos pos' -> let pos2 = valid_parse_pair_inv_spec h p1 (p2 `parse_pair` p3) b pos pos' in let pos3 = valid_parse_pair_inv_spec h p2 p3 b pos2 pos' in valid_parse_pair p1 p2 h b pos pos2 pos3; valid_parse_pair (p1 `parse_pair` p2) p3 h b pos pos3 pos' ); valid_rewrite_size = (fun _ -> ()); } let valid_rewrite_parse_pair_compat_l (p: parser) (p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) ($v: valid_rewrite_t p1 p2 precond f) : Tot (valid_rewrite_t (p `parse_pair` p1) (p `parse_pair` p2) (fun (_, x) -> precond x) (fun (x, y) -> (x, f y))) = { valid_rewrite_valid = (fun h b pos pos' -> let posq = valid_parse_pair_inv_spec h p p1 b pos pos' in v.valid_rewrite_valid h b posq pos'; valid_parse_pair p p2 h b pos posq pos' ); valid_rewrite_size = (fun xy -> let (_, x) = (xy <: Parser?.t (p `parse_pair` p1)) in v.valid_rewrite_size x ); }
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.LowParse.parser -> p1: LowParseWriters.LowParse.parser -> p2: LowParseWriters.LowParse.parser -> precond: LowParseWriters.pre_t p1 -> f: (x: Parser?.t p1 {precond x} -> Prims.GTot (Parser?.t p2)) -> $v: LowParseWriters.valid_rewrite_t p1 p2 precond f -> LowParseWriters.valid_rewrite_t (LowParseWriters.LowParse.parse_pair p1 p) (LowParseWriters.LowParse.parse_pair p2 p) (fun _ -> (let FStar.Pervasives.Native.Mktuple2 #_ #_ x _ = _ in precond x) <: Type0) (fun _ -> (let FStar.Pervasives.Native.Mktuple2 #_ #_ x y = _ in f x, y) <: Parser?.t (LowParseWriters.LowParse.parse_pair p2 p))
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.valid_rewrite_t", "LowParseWriters.Mkvalid_rewrite_t", "LowParseWriters.LowParse.parse_pair", "FStar.Pervasives.Native.Mktuple2", "FStar.Monotonic.HyperStack.mem", "LowStar.Buffer.buffer", "FStar.UInt8.t", "FStar.UInt32.t", "LowParseWriters.LowParse.valid_parse_pair", "Prims.unit", "LowParseWriters.__proj__Mkvalid_rewrite_t__item__valid_rewrite_valid", "LowParseWriters.LowParse.valid_parse_pair_inv_spec", "LowParseWriters.__proj__Mkvalid_rewrite_t__item__valid_rewrite_size" ]
[]
false
false
false
false
false
let valid_rewrite_parse_pair_compat_r (p p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 {precond x} -> GTot (Parser?.t p2))) ($v: valid_rewrite_t p1 p2 precond f) : Tot (valid_rewrite_t (p1 `parse_pair` p) (p2 `parse_pair` p) (fun (x, _) -> precond x) (fun (x, y) -> (f x, y))) =
{ valid_rewrite_valid = (fun h b pos pos' -> let posp = valid_parse_pair_inv_spec h p1 p b pos pos' in v.valid_rewrite_valid h b pos posp; valid_parse_pair p2 p h b pos posp pos'); valid_rewrite_size = (fun xy -> let x, _ = (xy <: Parser?.t (p1 `parse_pair` p)) in v.valid_rewrite_size x) }
false
LowParseWriters.fsti
LowParseWriters.valid_rewrite_parse_pair_compat_l
val valid_rewrite_parse_pair_compat_l (p p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 {precond x} -> GTot (Parser?.t p2))) ($v: valid_rewrite_t p1 p2 precond f) : Tot (valid_rewrite_t (p `parse_pair` p1) (p `parse_pair` p2) (fun (_, x) -> precond x) (fun (x, y) -> (x, f y)))
val valid_rewrite_parse_pair_compat_l (p p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 {precond x} -> GTot (Parser?.t p2))) ($v: valid_rewrite_t p1 p2 precond f) : Tot (valid_rewrite_t (p `parse_pair` p1) (p `parse_pair` p2) (fun (_, x) -> precond x) (fun (x, y) -> (x, f y)))
let valid_rewrite_parse_pair_compat_l (p: parser) (p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) ($v: valid_rewrite_t p1 p2 precond f) : Tot (valid_rewrite_t (p `parse_pair` p1) (p `parse_pair` p2) (fun (_, x) -> precond x) (fun (x, y) -> (x, f y))) = { valid_rewrite_valid = (fun h b pos pos' -> let posq = valid_parse_pair_inv_spec h p p1 b pos pos' in v.valid_rewrite_valid h b posq pos'; valid_parse_pair p p2 h b pos posq pos' ); valid_rewrite_size = (fun xy -> let (_, x) = (xy <: Parser?.t (p `parse_pair` p1)) in v.valid_rewrite_size x ); }
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 1, "end_line": 1914, "start_col": 0, "start_line": 1897 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in inline_for_extraction val recast_writer_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f)) inline_for_extraction let recast_writer_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) = mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f) inline_for_extraction let recast_writer (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: unit -> EWrite a r_in r_out pre post post_err l) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l = recast_writer_repr a r_in r_out pre post post_err l f () inline_for_extraction let frame' (a: Type) (fr: parser) (p: parser) (l: memory_invariant) (f: unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l ) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout) ) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ()) ) l = frame _ _ _ _ _ _ _ (fun _ -> recast_writer _ _ _ _ _ _ _ f) unfold let frame2_pre (frame: parser) (ppre: parser) (pre: pre_t ppre) : Tot (pre_t (frame `parse_pair` ppre)) = fun (_, x) -> pre x unfold let frame2_post (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) : Tot (post_t a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre)) = fun (v_frame, v_in) v (v_frame', v_out) -> v_frame == v_frame' /\ post v_in v v_out unfold let frame2_post_err (frame: parser) (ppre: parser) (pre: pre_t ppre) (post_err: post_err_t ppre pre) : Tot (post_err_t (frame `parse_pair` ppre) (frame2_pre frame ppre pre)) = fun (_, x) -> post_err x let frame2_spec (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_spec a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err)) = fun (fr, w_in) -> match destr_repr_spec a ppre p pre post post_err l inner w_in with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame2_impl (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_impl a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner)) inline_for_extraction let frame2_repr (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (unit -> EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l) = mk_repr a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner) (frame2_impl a frame ppre pre p post post_err l inner) inline_for_extraction let frame2 (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) ($inner: unit -> EWrite a ppre p pre post post_err l) : EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l = frame2_repr a frame ppre pre p post post_err l inner () noeq [@erasable] // very important, otherwise KaRaMeL will fail with argument typing type valid_rewrite_t (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) = { valid_rewrite_valid: (h: HS.mem) -> (b: B.buffer U8.t) -> (pos: U32.t) -> (pos' : U32.t) -> Lemma (requires ( valid_pos p1 h b pos pos' /\ precond (contents p1 h b pos pos') )) (ensures ( valid_pos p1 h b pos pos' /\ ( let x = contents p1 h b pos pos' in precond x /\ valid_pos p2 h b pos pos' /\ contents p2 h b pos pos' == f x ))); valid_rewrite_size: (x: Parser?.t p1 { precond x }) -> Lemma (size p1 x == size p2 (f x)) } let valid_rewrite_implies (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (precond' : pre_t p1) (f' : (x: Parser?.t p1 { precond' x }) -> GTot (Parser?.t p2)) : Pure (valid_rewrite_t p1 p2 precond' f') (requires ( (forall (x: Parser?.t p1) . precond' x ==> precond x) /\ (forall (x: Parser?.t p1 { precond' x }) . f' x == f x) )) (ensures (fun _ -> True)) = { valid_rewrite_valid = (fun h b pos pos' -> v.valid_rewrite_valid h b pos pos'); valid_rewrite_size = (fun x -> v.valid_rewrite_size x); } let valid_rewrite_compose (p1: parser) (p2: parser) (precond12: pre_t p1) (f12: (x: Parser?.t p1 { precond12 x }) -> GTot (Parser?.t p2)) (v12: valid_rewrite_t p1 p2 precond12 f12) (p3: parser) (precond23: pre_t p2) (f23: (x: Parser?.t p2 { precond23 x }) -> GTot (Parser?.t p3)) (v23: valid_rewrite_t p2 p3 precond23 f23) : Tot (valid_rewrite_t p1 p3 (fun x -> precond12 x /\ precond23 (f12 x)) (fun x -> f23 (f12 x))) = { valid_rewrite_valid = (fun h b pos pos' -> v12.valid_rewrite_valid h b pos pos'; v23.valid_rewrite_valid h b pos pos' ); valid_rewrite_size = (fun x -> v12.valid_rewrite_size x; v23.valid_rewrite_size (f12 x) ); } let valid_rewrite_spec (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) : Tot (repr_spec unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False)) = fun vin -> v.valid_rewrite_size vin; Correct ((), f vin) inline_for_extraction val valid_rewrite_impl (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) : Tot (repr_impl unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False) inv (valid_rewrite_spec p1 p2 precond f v)) inline_for_extraction let valid_rewrite_repr (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) : Tot (repr unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False) inv) = Repr _ (valid_rewrite_impl p1 p2 precond f v inv) inline_for_extraction let valid_rewrite (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (inv: memory_invariant) (v: valid_rewrite_t p1 p2 precond f) : Write unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) inv = EWrite?.reflect (valid_rewrite_repr p1 p2 precond f v inv) inline_for_extraction val cast (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) (x1: ptr p1 inv { precond (deref_spec x1) }) : Tot (x2: ptr p2 inv { deref_spec x2 == f (deref_spec x1) }) let valid_rewrite_parse_pair_assoc_1 (p1 p2 p3: parser) : Tot (valid_rewrite_t ((p1 `parse_pair` p2) `parse_pair` p3) (p1 `parse_pair` (p2 `parse_pair` p3)) (fun _ -> True) (fun ((x1, x2), x3) -> (x1, (x2, x3)))) = { valid_rewrite_valid = (fun h b pos pos' -> let pos3 = valid_parse_pair_inv_spec h (p1 `parse_pair` p2) p3 b pos pos' in let pos2 = valid_parse_pair_inv_spec h p1 p2 b pos pos3 in valid_parse_pair p2 p3 h b pos2 pos3 pos'; valid_parse_pair p1 (p2 `parse_pair` p3) h b pos pos2 pos' ); valid_rewrite_size = (fun _ -> ()); } let valid_rewrite_parse_pair_assoc_2 (p1 p2 p3: parser) : Tot (valid_rewrite_t (p1 `parse_pair` (p2 `parse_pair` p3)) ((p1 `parse_pair` p2) `parse_pair` p3) (fun _ -> True) (fun (x1, (x2, x3)) -> ((x1, x2), x3))) = { valid_rewrite_valid = (fun h b pos pos' -> let pos2 = valid_parse_pair_inv_spec h p1 (p2 `parse_pair` p3) b pos pos' in let pos3 = valid_parse_pair_inv_spec h p2 p3 b pos2 pos' in valid_parse_pair p1 p2 h b pos pos2 pos3; valid_parse_pair (p1 `parse_pair` p2) p3 h b pos pos3 pos' ); valid_rewrite_size = (fun _ -> ()); }
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.LowParse.parser -> p1: LowParseWriters.LowParse.parser -> p2: LowParseWriters.LowParse.parser -> precond: LowParseWriters.pre_t p1 -> f: (x: Parser?.t p1 {precond x} -> Prims.GTot (Parser?.t p2)) -> $v: LowParseWriters.valid_rewrite_t p1 p2 precond f -> LowParseWriters.valid_rewrite_t (LowParseWriters.LowParse.parse_pair p p1) (LowParseWriters.LowParse.parse_pair p p2) (fun _ -> (let FStar.Pervasives.Native.Mktuple2 #_ #_ _ x = _ in precond x) <: Type0) (fun _ -> (let FStar.Pervasives.Native.Mktuple2 #_ #_ x y = _ in x, f y) <: Parser?.t (LowParseWriters.LowParse.parse_pair p p2))
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.valid_rewrite_t", "LowParseWriters.Mkvalid_rewrite_t", "LowParseWriters.LowParse.parse_pair", "FStar.Pervasives.Native.Mktuple2", "FStar.Monotonic.HyperStack.mem", "LowStar.Buffer.buffer", "FStar.UInt8.t", "FStar.UInt32.t", "LowParseWriters.LowParse.valid_parse_pair", "Prims.unit", "LowParseWriters.__proj__Mkvalid_rewrite_t__item__valid_rewrite_valid", "LowParseWriters.LowParse.valid_parse_pair_inv_spec", "LowParseWriters.__proj__Mkvalid_rewrite_t__item__valid_rewrite_size" ]
[]
false
false
false
false
false
let valid_rewrite_parse_pair_compat_l (p p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 {precond x} -> GTot (Parser?.t p2))) ($v: valid_rewrite_t p1 p2 precond f) : Tot (valid_rewrite_t (p `parse_pair` p1) (p `parse_pair` p2) (fun (_, x) -> precond x) (fun (x, y) -> (x, f y))) =
{ valid_rewrite_valid = (fun h b pos pos' -> let posq = valid_parse_pair_inv_spec h p p1 b pos pos' in v.valid_rewrite_valid h b posq pos'; valid_parse_pair p p2 h b pos posq pos'); valid_rewrite_size = (fun xy -> let _, x = (xy <: Parser?.t (p `parse_pair` p1)) in v.valid_rewrite_size x) }
false
LowParseWriters.fsti
LowParseWriters.frame2
val frame2 (a: Type) (frame ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) ($inner: (unit -> EWrite a ppre p pre post post_err l)) : EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l
val frame2 (a: Type) (frame ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) ($inner: (unit -> EWrite a ppre p pre post post_err l)) : EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l
let frame2 (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) ($inner: unit -> EWrite a ppre p pre post post_err l) : EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l = frame2_repr a frame ppre pre p post post_err l inner ()
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 57, "end_line": 1741, "start_col": 0, "start_line": 1730 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in inline_for_extraction val recast_writer_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f)) inline_for_extraction let recast_writer_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) = mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f) inline_for_extraction let recast_writer (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: unit -> EWrite a r_in r_out pre post post_err l) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l = recast_writer_repr a r_in r_out pre post post_err l f () inline_for_extraction let frame' (a: Type) (fr: parser) (p: parser) (l: memory_invariant) (f: unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l ) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout) ) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ()) ) l = frame _ _ _ _ _ _ _ (fun _ -> recast_writer _ _ _ _ _ _ _ f) unfold let frame2_pre (frame: parser) (ppre: parser) (pre: pre_t ppre) : Tot (pre_t (frame `parse_pair` ppre)) = fun (_, x) -> pre x unfold let frame2_post (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) : Tot (post_t a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre)) = fun (v_frame, v_in) v (v_frame', v_out) -> v_frame == v_frame' /\ post v_in v v_out unfold let frame2_post_err (frame: parser) (ppre: parser) (pre: pre_t ppre) (post_err: post_err_t ppre pre) : Tot (post_err_t (frame `parse_pair` ppre) (frame2_pre frame ppre pre)) = fun (_, x) -> post_err x let frame2_spec (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_spec a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err)) = fun (fr, w_in) -> match destr_repr_spec a ppre p pre post post_err l inner w_in with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame2_impl (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_impl a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner)) inline_for_extraction let frame2_repr (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (unit -> EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l) = mk_repr a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner) (frame2_impl a frame ppre pre p post post_err l inner)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> frame: LowParseWriters.LowParse.parser -> ppre: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t ppre -> p: LowParseWriters.LowParse.parser -> post: LowParseWriters.post_t a ppre p pre -> post_err: LowParseWriters.post_err_t ppre pre -> l: LowParseWriters.memory_invariant -> $inner: (_: Prims.unit -> LowParseWriters.EWrite a) -> LowParseWriters.EWrite a
LowParseWriters.EWrite
[]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.memory_invariant", "Prims.unit", "LowParseWriters.frame2_repr", "LowParseWriters.LowParse.parse_pair", "LowParseWriters.frame_out", "LowParseWriters.frame2_pre", "LowParseWriters.frame2_post", "LowParseWriters.frame2_post_err" ]
[]
false
true
false
false
false
let frame2 (a: Type) (frame ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) ($inner: (unit -> EWrite a ppre p pre post post_err l)) : EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l =
frame2_repr a frame ppre pre p post post_err l inner ()
false
Pulse.C.Types.UserStruct.fsti
Pulse.C.Types.UserStruct.set_aux
val set_aux (#t: Type) (sd: struct_def t) (x: t) (f: field_t sd.fields) (v: sd.field_desc.fd_type f) (f': field_t sd.fields) : Tot (sd.field_desc.fd_type f')
val set_aux (#t: Type) (sd: struct_def t) (x: t) (f: field_t sd.fields) (v: sd.field_desc.fd_type f) (f': field_t sd.fields) : Tot (sd.field_desc.fd_type f')
let set_aux (#t: Type) (sd: struct_def t) (x: t) (f: field_t sd.fields) (v: sd.field_desc.fd_type f) (f': field_t sd.fields) : Tot (sd.field_desc.fd_type f') = if f = f' then v else sd.get x f'
{ "file_name": "share/steel/examples/pulse/lib/c/Pulse.C.Types.UserStruct.fsti", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 35, "end_line": 57, "start_col": 0, "start_line": 54 }
module Pulse.C.Types.UserStruct open Pulse.Lib.Pervasives open Pulse.C.Types.Struct.Aux module Set = FStar.Set (* This library allows the user to define their own struct type, with a constructor from field values, and a destructor to field values for each field. This may be necessary for recursive structs *) let set_def (#t: eqtype) (s: FStar.Set.set t) (x: t) : Tot bool = FStar.Set.mem x s noextract let nonempty_set (t: eqtype) = (s: Set.set t { exists x . set_def s x == true }) noextract let set_snoc // for associativity reasons (#t: eqtype) (q: FStar.Set.set t) (a: t) : Pure (nonempty_set t) (requires True) (ensures (fun s -> (forall (x: t). {:pattern FStar.Set.mem x s} FStar.Set.mem x s == (x = a || FStar.Set.mem x q)) )) = q `FStar.Set.union` FStar.Set.singleton a [@@noextract_to "krml"] let field_t (s: Set.set string) : Tot eqtype = (f: string { Set.mem f s }) [@@noextract_to "krml"; norm_field_attr] inline_for_extraction // for field_desc.fd_type noeq type struct_def (t: Type) = { fields: Set.set string; field_desc: field_description_gen_t (field_t fields); mk: ((f: field_t fields) -> Tot (field_desc.fd_type f)) -> Tot t; get: (t -> (f: field_t fields) -> Tot (field_desc.fd_type f)); get_mk: (phi: ((f: field_t fields) -> Tot (field_desc.fd_type f))) -> (f: field_t fields) -> Lemma (get (mk phi) f == phi f); extensionality: (x1: t) -> (x2: t) -> ((f: field_t fields) -> Lemma (get x1 f == get x2 f)) -> Lemma (x1 == x2); } let nonempty_set_nonempty_type (x: string) (s: Set.set string) : Lemma (requires (x `Set.mem` s)) (ensures (exists (x: field_t s) . True)) = Classical.exists_intro (fun (_: field_t s) -> True) x
{ "checked_file": "/", "dependencies": [ "Pulse.Lib.Pervasives.fst.checked", "Pulse.C.Types.Struct.Aux.fsti.checked", "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Pulse.C.Types.UserStruct.fsti" }
[ { "abbrev": true, "full_module": "FStar.Set", "short_module": "Set" }, { "abbrev": false, "full_module": "Pulse.C.Types.Struct.Aux", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Lib.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Pulse.C.Types", "short_module": null }, { "abbrev": false, "full_module": "Pulse.C.Types", "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
sd: Pulse.C.Types.UserStruct.struct_def t -> x: t -> f: Pulse.C.Types.UserStruct.field_t (Mkstruct_def?.fields sd) -> v: Mkfield_description_gen_t?.fd_type (Mkstruct_def?.field_desc sd) f -> f': Pulse.C.Types.UserStruct.field_t (Mkstruct_def?.fields sd) -> Mkfield_description_gen_t?.fd_type (Mkstruct_def?.field_desc sd) f'
Prims.Tot
[ "total" ]
[]
[ "Pulse.C.Types.UserStruct.struct_def", "Pulse.C.Types.UserStruct.field_t", "Pulse.C.Types.UserStruct.__proj__Mkstruct_def__item__fields", "Pulse.C.Types.Struct.Aux.__proj__Mkfield_description_gen_t__item__fd_type", "Pulse.C.Types.UserStruct.__proj__Mkstruct_def__item__field_desc", "Prims.op_Equality", "Prims.bool", "Pulse.C.Types.UserStruct.__proj__Mkstruct_def__item__get" ]
[]
false
false
false
false
false
let set_aux (#t: Type) (sd: struct_def t) (x: t) (f: field_t sd.fields) (v: sd.field_desc.fd_type f) (f': field_t sd.fields) : Tot (sd.field_desc.fd_type f') =
if f = f' then v else sd.get x f'
false
LowParseWriters.fsti
LowParseWriters.frame2_repr
val frame2_repr (a: Type) (frame ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: (unit -> EWrite a ppre p pre post post_err l)) : Tot (unit -> EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l)
val frame2_repr (a: Type) (frame ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: (unit -> EWrite a ppre p pre post post_err l)) : Tot (unit -> EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l)
let frame2_repr (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (unit -> EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l) = mk_repr a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner) (frame2_impl a frame ppre pre p post post_err l inner)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 58, "end_line": 1727, "start_col": 0, "start_line": 1713 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in inline_for_extraction val recast_writer_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f)) inline_for_extraction let recast_writer_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) = mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f) inline_for_extraction let recast_writer (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: unit -> EWrite a r_in r_out pre post post_err l) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l = recast_writer_repr a r_in r_out pre post post_err l f () inline_for_extraction let frame' (a: Type) (fr: parser) (p: parser) (l: memory_invariant) (f: unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l ) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout) ) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ()) ) l = frame _ _ _ _ _ _ _ (fun _ -> recast_writer _ _ _ _ _ _ _ f) unfold let frame2_pre (frame: parser) (ppre: parser) (pre: pre_t ppre) : Tot (pre_t (frame `parse_pair` ppre)) = fun (_, x) -> pre x unfold let frame2_post (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) : Tot (post_t a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre)) = fun (v_frame, v_in) v (v_frame', v_out) -> v_frame == v_frame' /\ post v_in v v_out unfold let frame2_post_err (frame: parser) (ppre: parser) (pre: pre_t ppre) (post_err: post_err_t ppre pre) : Tot (post_err_t (frame `parse_pair` ppre) (frame2_pre frame ppre pre)) = fun (_, x) -> post_err x let frame2_spec (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_spec a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err)) = fun (fr, w_in) -> match destr_repr_spec a ppre p pre post post_err l inner w_in with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame2_impl (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_impl a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> frame: LowParseWriters.LowParse.parser -> ppre: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t ppre -> p: LowParseWriters.LowParse.parser -> post: LowParseWriters.post_t a ppre p pre -> post_err: LowParseWriters.post_err_t ppre pre -> l: LowParseWriters.memory_invariant -> inner: (_: Prims.unit -> LowParseWriters.EWrite a) -> _: Prims.unit -> LowParseWriters.EWrite a
LowParseWriters.EWrite
[]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.memory_invariant", "Prims.unit", "LowParseWriters.mk_repr", "LowParseWriters.LowParse.parse_pair", "LowParseWriters.frame_out", "LowParseWriters.frame2_pre", "LowParseWriters.frame2_post", "LowParseWriters.frame2_post_err", "LowParseWriters.frame2_spec", "LowParseWriters.frame2_impl" ]
[]
false
true
false
false
false
let frame2_repr (a: Type) (frame ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: (unit -> EWrite a ppre p pre post post_err l)) : Tot (unit -> EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l) =
mk_repr a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner) (frame2_impl a frame ppre pre p post post_err l inner)
false
LowParseWriters.fsti
LowParseWriters.valid_rewrite_parse_pair_assoc_1
val valid_rewrite_parse_pair_assoc_1 (p1 p2 p3: parser) : Tot (valid_rewrite_t ((p1 `parse_pair` p2) `parse_pair` p3) (p1 `parse_pair` (p2 `parse_pair` p3)) (fun _ -> True) (fun ((x1, x2), x3) -> (x1, (x2, x3))))
val valid_rewrite_parse_pair_assoc_1 (p1 p2 p3: parser) : Tot (valid_rewrite_t ((p1 `parse_pair` p2) `parse_pair` p3) (p1 `parse_pair` (p2 `parse_pair` p3)) (fun _ -> True) (fun ((x1, x2), x3) -> (x1, (x2, x3))))
let valid_rewrite_parse_pair_assoc_1 (p1 p2 p3: parser) : Tot (valid_rewrite_t ((p1 `parse_pair` p2) `parse_pair` p3) (p1 `parse_pair` (p2 `parse_pair` p3)) (fun _ -> True) (fun ((x1, x2), x3) -> (x1, (x2, x3)))) = { valid_rewrite_valid = (fun h b pos pos' -> let pos3 = valid_parse_pair_inv_spec h (p1 `parse_pair` p2) p3 b pos pos' in let pos2 = valid_parse_pair_inv_spec h p1 p2 b pos pos3 in valid_parse_pair p2 p3 h b pos2 pos3 pos'; valid_parse_pair p1 (p2 `parse_pair` p3) h b pos pos2 pos' ); valid_rewrite_size = (fun _ -> ()); }
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 3, "end_line": 1882, "start_col": 0, "start_line": 1871 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in inline_for_extraction val recast_writer_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f)) inline_for_extraction let recast_writer_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) = mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f) inline_for_extraction let recast_writer (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: unit -> EWrite a r_in r_out pre post post_err l) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l = recast_writer_repr a r_in r_out pre post post_err l f () inline_for_extraction let frame' (a: Type) (fr: parser) (p: parser) (l: memory_invariant) (f: unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l ) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout) ) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ()) ) l = frame _ _ _ _ _ _ _ (fun _ -> recast_writer _ _ _ _ _ _ _ f) unfold let frame2_pre (frame: parser) (ppre: parser) (pre: pre_t ppre) : Tot (pre_t (frame `parse_pair` ppre)) = fun (_, x) -> pre x unfold let frame2_post (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) : Tot (post_t a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre)) = fun (v_frame, v_in) v (v_frame', v_out) -> v_frame == v_frame' /\ post v_in v v_out unfold let frame2_post_err (frame: parser) (ppre: parser) (pre: pre_t ppre) (post_err: post_err_t ppre pre) : Tot (post_err_t (frame `parse_pair` ppre) (frame2_pre frame ppre pre)) = fun (_, x) -> post_err x let frame2_spec (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_spec a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err)) = fun (fr, w_in) -> match destr_repr_spec a ppre p pre post post_err l inner w_in with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame2_impl (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_impl a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner)) inline_for_extraction let frame2_repr (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (unit -> EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l) = mk_repr a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner) (frame2_impl a frame ppre pre p post post_err l inner) inline_for_extraction let frame2 (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) ($inner: unit -> EWrite a ppre p pre post post_err l) : EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l = frame2_repr a frame ppre pre p post post_err l inner () noeq [@erasable] // very important, otherwise KaRaMeL will fail with argument typing type valid_rewrite_t (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) = { valid_rewrite_valid: (h: HS.mem) -> (b: B.buffer U8.t) -> (pos: U32.t) -> (pos' : U32.t) -> Lemma (requires ( valid_pos p1 h b pos pos' /\ precond (contents p1 h b pos pos') )) (ensures ( valid_pos p1 h b pos pos' /\ ( let x = contents p1 h b pos pos' in precond x /\ valid_pos p2 h b pos pos' /\ contents p2 h b pos pos' == f x ))); valid_rewrite_size: (x: Parser?.t p1 { precond x }) -> Lemma (size p1 x == size p2 (f x)) } let valid_rewrite_implies (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (precond' : pre_t p1) (f' : (x: Parser?.t p1 { precond' x }) -> GTot (Parser?.t p2)) : Pure (valid_rewrite_t p1 p2 precond' f') (requires ( (forall (x: Parser?.t p1) . precond' x ==> precond x) /\ (forall (x: Parser?.t p1 { precond' x }) . f' x == f x) )) (ensures (fun _ -> True)) = { valid_rewrite_valid = (fun h b pos pos' -> v.valid_rewrite_valid h b pos pos'); valid_rewrite_size = (fun x -> v.valid_rewrite_size x); } let valid_rewrite_compose (p1: parser) (p2: parser) (precond12: pre_t p1) (f12: (x: Parser?.t p1 { precond12 x }) -> GTot (Parser?.t p2)) (v12: valid_rewrite_t p1 p2 precond12 f12) (p3: parser) (precond23: pre_t p2) (f23: (x: Parser?.t p2 { precond23 x }) -> GTot (Parser?.t p3)) (v23: valid_rewrite_t p2 p3 precond23 f23) : Tot (valid_rewrite_t p1 p3 (fun x -> precond12 x /\ precond23 (f12 x)) (fun x -> f23 (f12 x))) = { valid_rewrite_valid = (fun h b pos pos' -> v12.valid_rewrite_valid h b pos pos'; v23.valid_rewrite_valid h b pos pos' ); valid_rewrite_size = (fun x -> v12.valid_rewrite_size x; v23.valid_rewrite_size (f12 x) ); } let valid_rewrite_spec (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) : Tot (repr_spec unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False)) = fun vin -> v.valid_rewrite_size vin; Correct ((), f vin) inline_for_extraction val valid_rewrite_impl (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) : Tot (repr_impl unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False) inv (valid_rewrite_spec p1 p2 precond f v)) inline_for_extraction let valid_rewrite_repr (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) : Tot (repr unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False) inv) = Repr _ (valid_rewrite_impl p1 p2 precond f v inv) inline_for_extraction let valid_rewrite (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (inv: memory_invariant) (v: valid_rewrite_t p1 p2 precond f) : Write unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) inv = EWrite?.reflect (valid_rewrite_repr p1 p2 precond f v inv) inline_for_extraction val cast (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) (x1: ptr p1 inv { precond (deref_spec x1) }) : Tot (x2: ptr p2 inv { deref_spec x2 == f (deref_spec x1) })
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.LowParse.parser -> p2: LowParseWriters.LowParse.parser -> p3: LowParseWriters.LowParse.parser -> LowParseWriters.valid_rewrite_t (LowParseWriters.LowParse.parse_pair (LowParseWriters.LowParse.parse_pair p1 p2) p3) (LowParseWriters.LowParse.parse_pair p1 (LowParseWriters.LowParse.parse_pair p2 p3)) (fun _ -> Prims.l_True) (fun _ -> (let FStar.Pervasives.Native.Mktuple2 #_ #_ (FStar.Pervasives.Native.Mktuple2 #_ #_ x1 x2) x3 = _ in x1, (x2, x3)) <: Parser?.t (LowParseWriters.LowParse.parse_pair p1 (LowParseWriters.LowParse.parse_pair p2 p3)))
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.Mkvalid_rewrite_t", "LowParseWriters.LowParse.parse_pair", "LowParseWriters.LowParse.__proj__Parser__item__t", "Prims.l_True", "FStar.Pervasives.Native.Mktuple2", "FStar.Monotonic.HyperStack.mem", "LowStar.Buffer.buffer", "FStar.UInt8.t", "FStar.UInt32.t", "LowParseWriters.LowParse.valid_parse_pair", "Prims.unit", "LowParseWriters.LowParse.valid_parse_pair_inv_spec", "LowParseWriters.valid_rewrite_t" ]
[]
false
false
false
false
false
let valid_rewrite_parse_pair_assoc_1 (p1 p2 p3: parser) : Tot (valid_rewrite_t ((p1 `parse_pair` p2) `parse_pair` p3) (p1 `parse_pair` (p2 `parse_pair` p3)) (fun _ -> True) (fun ((x1, x2), x3) -> (x1, (x2, x3)))) =
{ valid_rewrite_valid = (fun h b pos pos' -> let pos3 = valid_parse_pair_inv_spec h (p1 `parse_pair` p2) p3 b pos pos' in let pos2 = valid_parse_pair_inv_spec h p1 p2 b pos pos3 in valid_parse_pair p2 p3 h b pos2 pos3 pos'; valid_parse_pair p1 (p2 `parse_pair` p3) h b pos pos2 pos'); valid_rewrite_size = (fun _ -> ()) }
false
Pulse.C.Types.UserStruct.fsti
Pulse.C.Types.UserStruct.struct_field
val struct_field (#t: Type) (#sd: struct_def t) (#v: Ghost.erased t) (r: ref (struct_typedef sd)) (field: field_t sd.fields) : stt (ref #(norm norm_field_steps (sd.field_desc.fd_type field)) (sd.field_desc.fd_typedef field)) (pts_to r v) (fun r' -> (has_struct_field r field r' ** pts_to r (set sd v field (unknown (sd.field_desc.fd_typedef field)))) ** pts_to #(norm norm_field_steps (sd.field_desc.fd_type field)) r' (sd.get v field))
val struct_field (#t: Type) (#sd: struct_def t) (#v: Ghost.erased t) (r: ref (struct_typedef sd)) (field: field_t sd.fields) : stt (ref #(norm norm_field_steps (sd.field_desc.fd_type field)) (sd.field_desc.fd_typedef field)) (pts_to r v) (fun r' -> (has_struct_field r field r' ** pts_to r (set sd v field (unknown (sd.field_desc.fd_typedef field)))) ** pts_to #(norm norm_field_steps (sd.field_desc.fd_type field)) r' (sd.get v field))
let struct_field (#t: Type) (#sd: struct_def t) (#v: Ghost.erased t) (r: ref (struct_typedef sd)) (field: field_t sd.fields) : stt (ref #(norm norm_field_steps (sd.field_desc.fd_type field)) (sd.field_desc.fd_typedef field)) (pts_to r v) (fun r' -> has_struct_field r field r' ** pts_to r (set sd v field (unknown (sd.field_desc.fd_typedef field))) ** pts_to #(norm norm_field_steps (sd.field_desc.fd_type field)) r' (sd.get v field)) = struct_field0 (norm norm_field_steps (sd.field_desc.fd_type field)) r field (sd.field_desc.fd_typedef field)
{ "file_name": "share/steel/examples/pulse/lib/c/Pulse.C.Types.UserStruct.fsti", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 36, "end_line": 174, "start_col": 0, "start_line": 161 }
module Pulse.C.Types.UserStruct open Pulse.Lib.Pervasives open Pulse.C.Types.Struct.Aux module Set = FStar.Set (* This library allows the user to define their own struct type, with a constructor from field values, and a destructor to field values for each field. This may be necessary for recursive structs *) let set_def (#t: eqtype) (s: FStar.Set.set t) (x: t) : Tot bool = FStar.Set.mem x s noextract let nonempty_set (t: eqtype) = (s: Set.set t { exists x . set_def s x == true }) noextract let set_snoc // for associativity reasons (#t: eqtype) (q: FStar.Set.set t) (a: t) : Pure (nonempty_set t) (requires True) (ensures (fun s -> (forall (x: t). {:pattern FStar.Set.mem x s} FStar.Set.mem x s == (x = a || FStar.Set.mem x q)) )) = q `FStar.Set.union` FStar.Set.singleton a [@@noextract_to "krml"] let field_t (s: Set.set string) : Tot eqtype = (f: string { Set.mem f s }) [@@noextract_to "krml"; norm_field_attr] inline_for_extraction // for field_desc.fd_type noeq type struct_def (t: Type) = { fields: Set.set string; field_desc: field_description_gen_t (field_t fields); mk: ((f: field_t fields) -> Tot (field_desc.fd_type f)) -> Tot t; get: (t -> (f: field_t fields) -> Tot (field_desc.fd_type f)); get_mk: (phi: ((f: field_t fields) -> Tot (field_desc.fd_type f))) -> (f: field_t fields) -> Lemma (get (mk phi) f == phi f); extensionality: (x1: t) -> (x2: t) -> ((f: field_t fields) -> Lemma (get x1 f == get x2 f)) -> Lemma (x1 == x2); } let nonempty_set_nonempty_type (x: string) (s: Set.set string) : Lemma (requires (x `Set.mem` s)) (ensures (exists (x: field_t s) . True)) = Classical.exists_intro (fun (_: field_t s) -> True) x [@@noextract_to "krml"] let set_aux (#t: Type) (sd: struct_def t) (x: t) (f: field_t sd.fields) (v: sd.field_desc.fd_type f) (f': field_t sd.fields) : Tot (sd.field_desc.fd_type f') = if f = f' then v else sd.get x f' [@@noextract_to "krml"] let set (#t: Type) (sd: struct_def t) (x: t) (f: field_t sd.fields) (v: sd.field_desc.fd_type f) : Tot t = sd.mk (set_aux sd x f v) let get_set (#t: Type) (sd: struct_def t) (x: t) (f: field_t sd.fields) (v: sd.field_desc.fd_type f) (f' : field_t sd.fields) : Lemma (sd.get (set sd x f v) f' == (if f = f' then v else sd.get x f')) [SMTPat (sd.get (set sd x f v) f')] = sd.get_mk (set_aux sd x f v) f' [@@noextract_to "krml"] val struct_typedef (#t: Type) (sd: struct_def t) : Tot (typedef t) val has_struct_field (#t: Type) (#sd: struct_def t) (r: ref (struct_typedef sd)) (field: field_t sd.fields) (r': ref (sd.field_desc.fd_typedef field)) : Tot vprop val has_struct_field_dup (#t: Type) (#sd: struct_def t) (r: ref (struct_typedef sd)) (field: field_t sd.fields) (r': ref (sd.field_desc.fd_typedef field)) : stt_ghost unit (has_struct_field r field r') (fun _ -> has_struct_field r field r' ** has_struct_field r field r') val has_struct_field_inj (#t: Type) (#sd: struct_def t) (r: ref (struct_typedef sd)) (field: field_t sd.fields) (r1 r2: ref (sd.field_desc.fd_typedef field)) : stt_ghost unit (has_struct_field r field r1 ** has_struct_field r field r2) (fun _ -> has_struct_field r field r1 ** has_struct_field r field r2 ** ref_equiv r1 r2) val has_struct_field_equiv_from (#t: Type) (#sd: struct_def t) (r1: ref (struct_typedef sd)) (field: field_t sd.fields) (r': ref (sd.field_desc.fd_typedef field)) (r2: ref (struct_typedef sd)) : stt_ghost unit (ref_equiv r1 r2 ** has_struct_field r1 field r') (fun _ -> ref_equiv r1 r2 ** has_struct_field r2 field r') val has_struct_field_equiv_to (#t: Type) (#sd: struct_def t) (r: ref (struct_typedef sd)) (field: field_t sd.fields) (r1' r2': ref (sd.field_desc.fd_typedef field)) : stt_ghost unit (ref_equiv r1' r2' ** has_struct_field r field r1') (fun _ -> ref_equiv r1' r2' ** has_struct_field r field r2') val ghost_struct_field_focus (#t: Type) (#sd: struct_def t) (#v: Ghost.erased t) (r: ref (struct_typedef sd)) (field: field_t sd.fields) (r': ref (sd.field_desc.fd_typedef field)) : stt_ghost unit (has_struct_field r field r' ** pts_to r v) (fun _ -> has_struct_field r field r' ** pts_to r (set sd v field (unknown (sd.field_desc.fd_typedef field))) ** pts_to r' (sd.get v field)) val ghost_struct_field (#t: Type) (#sd: struct_def t) (#v: Ghost.erased t) (r: ref (struct_typedef sd)) (field: field_t sd.fields) : stt_ghost (Ghost.erased (ref (sd.field_desc.fd_typedef field))) (pts_to r v) (fun r' -> has_struct_field r field r' ** pts_to r (set sd v field (unknown (sd.field_desc.fd_typedef field))) ** pts_to r' (sd.get v field)) [@@noextract_to "krml"] // primitive val struct_field0 (#t: Type) (t': Type0) (#sd: struct_def t) (#v: Ghost.erased t) (r: ref (struct_typedef sd)) (field: field_t sd.fields) (td': typedef t' { t' == sd.field_desc.fd_type field /\ td' == sd.field_desc.fd_typedef field }) : stt (ref td') (pts_to r v) (fun r' -> has_struct_field r field (coerce_eq () r') ** pts_to r (set sd (Ghost.reveal v) field (unknown (sd.field_desc.fd_typedef field))) ** pts_to #_ #(sd.field_desc.fd_typedef field) (coerce_eq () r') (sd.get (Ghost.reveal v) field))
{ "checked_file": "/", "dependencies": [ "Pulse.Lib.Pervasives.fst.checked", "Pulse.C.Types.Struct.Aux.fsti.checked", "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Pulse.C.Types.UserStruct.fsti" }
[ { "abbrev": true, "full_module": "FStar.Set", "short_module": "Set" }, { "abbrev": false, "full_module": "Pulse.C.Types.Struct.Aux", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Lib.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Pulse.C.Types", "short_module": null }, { "abbrev": false, "full_module": "Pulse.C.Types", "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: Pulse.C.Types.Base.ref (Pulse.C.Types.UserStruct.struct_typedef sd) -> field: Pulse.C.Types.UserStruct.field_t (Mkstruct_def?.fields sd) -> Pulse.Lib.Core.stt (Pulse.C.Types.Base.ref (Mkfield_description_gen_t?.fd_typedef (Mkstruct_def?.field_desc sd) field)) (Pulse.C.Types.Base.pts_to r v) (fun r' -> (Pulse.C.Types.UserStruct.has_struct_field r field r' ** Pulse.C.Types.Base.pts_to r (FStar.Ghost.hide (Pulse.C.Types.UserStruct.set sd (FStar.Ghost.reveal v) field (Pulse.C.Types.Base.unknown (Mkfield_description_gen_t?.fd_typedef (Mkstruct_def?.field_desc sd) field))))) ** Pulse.C.Types.Base.pts_to r' (FStar.Ghost.hide (Mkstruct_def?.get sd (FStar.Ghost.reveal v) field)))
Prims.Tot
[ "total" ]
[]
[ "Pulse.C.Types.UserStruct.struct_def", "FStar.Ghost.erased", "Pulse.C.Types.Base.ref", "Pulse.C.Types.UserStruct.struct_typedef", "Pulse.C.Types.UserStruct.field_t", "Pulse.C.Types.UserStruct.__proj__Mkstruct_def__item__fields", "Pulse.C.Types.UserStruct.struct_field0", "FStar.Pervasives.norm", "Pulse.C.Types.Base.norm_field_steps", "Pulse.C.Types.Struct.Aux.__proj__Mkfield_description_gen_t__item__fd_type", "Pulse.C.Types.UserStruct.__proj__Mkstruct_def__item__field_desc", "Pulse.C.Types.Struct.Aux.__proj__Mkfield_description_gen_t__item__fd_typedef", "Pulse.Lib.Core.stt", "Pulse.C.Types.Base.pts_to", "Pulse.Lib.Core.op_Star_Star", "Pulse.C.Types.UserStruct.has_struct_field", "FStar.Ghost.hide", "Pulse.C.Types.UserStruct.set", "FStar.Ghost.reveal", "Pulse.C.Types.Base.unknown", "Pulse.C.Types.UserStruct.__proj__Mkstruct_def__item__get", "Pulse.Lib.Core.vprop" ]
[]
false
false
false
false
false
let struct_field (#t: Type) (#sd: struct_def t) (#v: Ghost.erased t) (r: ref (struct_typedef sd)) (field: field_t sd.fields) : stt (ref #(norm norm_field_steps (sd.field_desc.fd_type field)) (sd.field_desc.fd_typedef field)) (pts_to r v) (fun r' -> (has_struct_field r field r' ** pts_to r (set sd v field (unknown (sd.field_desc.fd_typedef field)))) ** pts_to #(norm norm_field_steps (sd.field_desc.fd_type field)) r' (sd.get v field)) =
struct_field0 (norm norm_field_steps (sd.field_desc.fd_type field)) r field (sd.field_desc.fd_typedef field)
false
Pulse.C.Types.UserStruct.fsti
Pulse.C.Types.UserStruct.get_set
val get_set (#t: Type) (sd: struct_def t) (x: t) (f: field_t sd.fields) (v: sd.field_desc.fd_type f) (f': field_t sd.fields) : Lemma (sd.get (set sd x f v) f' == (if f = f' then v else sd.get x f')) [SMTPat (sd.get (set sd x f v) f')]
val get_set (#t: Type) (sd: struct_def t) (x: t) (f: field_t sd.fields) (v: sd.field_desc.fd_type f) (f': field_t sd.fields) : Lemma (sd.get (set sd x f v) f' == (if f = f' then v else sd.get x f')) [SMTPat (sd.get (set sd x f v) f')]
let get_set (#t: Type) (sd: struct_def t) (x: t) (f: field_t sd.fields) (v: sd.field_desc.fd_type f) (f' : field_t sd.fields) : Lemma (sd.get (set sd x f v) f' == (if f = f' then v else sd.get x f')) [SMTPat (sd.get (set sd x f v) f')] = sd.get_mk (set_aux sd x f v) f'
{ "file_name": "share/steel/examples/pulse/lib/c/Pulse.C.Types.UserStruct.fsti", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 33, "end_line": 66, "start_col": 0, "start_line": 63 }
module Pulse.C.Types.UserStruct open Pulse.Lib.Pervasives open Pulse.C.Types.Struct.Aux module Set = FStar.Set (* This library allows the user to define their own struct type, with a constructor from field values, and a destructor to field values for each field. This may be necessary for recursive structs *) let set_def (#t: eqtype) (s: FStar.Set.set t) (x: t) : Tot bool = FStar.Set.mem x s noextract let nonempty_set (t: eqtype) = (s: Set.set t { exists x . set_def s x == true }) noextract let set_snoc // for associativity reasons (#t: eqtype) (q: FStar.Set.set t) (a: t) : Pure (nonempty_set t) (requires True) (ensures (fun s -> (forall (x: t). {:pattern FStar.Set.mem x s} FStar.Set.mem x s == (x = a || FStar.Set.mem x q)) )) = q `FStar.Set.union` FStar.Set.singleton a [@@noextract_to "krml"] let field_t (s: Set.set string) : Tot eqtype = (f: string { Set.mem f s }) [@@noextract_to "krml"; norm_field_attr] inline_for_extraction // for field_desc.fd_type noeq type struct_def (t: Type) = { fields: Set.set string; field_desc: field_description_gen_t (field_t fields); mk: ((f: field_t fields) -> Tot (field_desc.fd_type f)) -> Tot t; get: (t -> (f: field_t fields) -> Tot (field_desc.fd_type f)); get_mk: (phi: ((f: field_t fields) -> Tot (field_desc.fd_type f))) -> (f: field_t fields) -> Lemma (get (mk phi) f == phi f); extensionality: (x1: t) -> (x2: t) -> ((f: field_t fields) -> Lemma (get x1 f == get x2 f)) -> Lemma (x1 == x2); } let nonempty_set_nonempty_type (x: string) (s: Set.set string) : Lemma (requires (x `Set.mem` s)) (ensures (exists (x: field_t s) . True)) = Classical.exists_intro (fun (_: field_t s) -> True) x [@@noextract_to "krml"] let set_aux (#t: Type) (sd: struct_def t) (x: t) (f: field_t sd.fields) (v: sd.field_desc.fd_type f) (f': field_t sd.fields) : Tot (sd.field_desc.fd_type f') = if f = f' then v else sd.get x f' [@@noextract_to "krml"] let set (#t: Type) (sd: struct_def t) (x: t) (f: field_t sd.fields) (v: sd.field_desc.fd_type f) : Tot t = sd.mk (set_aux sd x f v)
{ "checked_file": "/", "dependencies": [ "Pulse.Lib.Pervasives.fst.checked", "Pulse.C.Types.Struct.Aux.fsti.checked", "prims.fst.checked", "FStar.Set.fsti.checked", "FStar.Pervasives.fsti.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": false, "source_file": "Pulse.C.Types.UserStruct.fsti" }
[ { "abbrev": true, "full_module": "FStar.Set", "short_module": "Set" }, { "abbrev": false, "full_module": "Pulse.C.Types.Struct.Aux", "short_module": null }, { "abbrev": false, "full_module": "Pulse.Lib.Pervasives", "short_module": null }, { "abbrev": false, "full_module": "Pulse.C.Types", "short_module": null }, { "abbrev": false, "full_module": "Pulse.C.Types", "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
sd: Pulse.C.Types.UserStruct.struct_def t -> x: t -> f: Pulse.C.Types.UserStruct.field_t (Mkstruct_def?.fields sd) -> v: Mkfield_description_gen_t?.fd_type (Mkstruct_def?.field_desc sd) f -> f': Pulse.C.Types.UserStruct.field_t (Mkstruct_def?.fields sd) -> FStar.Pervasives.Lemma (ensures Mkstruct_def?.get sd (Pulse.C.Types.UserStruct.set sd x f v) f' == (match f = f' with | true -> v | _ -> Mkstruct_def?.get sd x f')) [SMTPat (Mkstruct_def?.get sd (Pulse.C.Types.UserStruct.set sd x f v) f')]
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "Pulse.C.Types.UserStruct.struct_def", "Pulse.C.Types.UserStruct.field_t", "Pulse.C.Types.UserStruct.__proj__Mkstruct_def__item__fields", "Pulse.C.Types.Struct.Aux.__proj__Mkfield_description_gen_t__item__fd_type", "Pulse.C.Types.UserStruct.__proj__Mkstruct_def__item__field_desc", "Pulse.C.Types.UserStruct.__proj__Mkstruct_def__item__get_mk", "Pulse.C.Types.UserStruct.set_aux", "Prims.unit", "Prims.l_True", "Prims.squash", "Prims.eq2", "Pulse.C.Types.UserStruct.__proj__Mkstruct_def__item__get", "Pulse.C.Types.UserStruct.set", "Prims.op_Equality", "Prims.bool", "Prims.Cons", "FStar.Pervasives.pattern", "FStar.Pervasives.smt_pat", "Prims.Nil" ]
[]
true
false
true
false
false
let get_set (#t: Type) (sd: struct_def t) (x: t) (f: field_t sd.fields) (v: sd.field_desc.fd_type f) (f': field_t sd.fields) : Lemma (sd.get (set sd x f v) f' == (if f = f' then v else sd.get x f')) [SMTPat (sd.get (set sd x f v) f')] =
sd.get_mk (set_aux sd x f v) f'
false
LowParseWriters.fsti
LowParseWriters.recast_writer_repr
val recast_writer_repr (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l)
val recast_writer_repr (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l)
let recast_writer_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) = mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 59, "end_line": 1618, "start_col": 0, "start_line": 1606 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in inline_for_extraction val recast_writer_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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 -> r_in: LowParseWriters.LowParse.parser -> r_out: LowParseWriters.LowParse.parser -> pre: LowParseWriters.pre_t r_in -> post: LowParseWriters.post_t a r_in r_out pre -> post_err: LowParseWriters.post_err_t r_in pre -> l: LowParseWriters.memory_invariant -> f: (_: Prims.unit -> LowParseWriters.EWrite a) -> _: Prims.unit -> LowParseWriters.EWrite a
LowParseWriters.EWrite
[]
[]
[ "LowParseWriters.LowParse.parser", "LowParseWriters.pre_t", "LowParseWriters.post_t", "LowParseWriters.post_err_t", "LowParseWriters.memory_invariant", "Prims.unit", "LowParseWriters.mk_repr", "LowParseWriters.recast_writer_post", "LowParseWriters.recast_writer_post_err", "LowParseWriters.recast_writer_spec", "LowParseWriters.recast_writer_impl" ]
[]
false
true
false
false
false
let recast_writer_repr (a: Type u#x) (r_in r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: (unit -> EWrite a r_in r_out pre post post_err l)) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) =
mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f)
false
CanonDeep.fst
CanonDeep.pack_fv'
val pack_fv' (n: name) : term
val pack_fv' (n: name) : term
let pack_fv' (n:name) : term = pack_ln (Tv_FVar (pack_fv n))
{ "file_name": "examples/native_tactics/CanonDeep.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 60, "end_line": 142, "start_col": 0, "start_line": 142 }
(* Copyright 2008-2018 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 CanonDeep open FStar.Tactics.V2 open FStar.Reflection.V2 open FStar.Reflection.V2.Arith open FStar.Mul module O = FStar.Order (* TODO: The implementation from FStar.Tactics.Canon seems *very* naive: - it repeatedly turns terms into exprs, but that's fixed below - more importantly, a lot of work seems to be done repeatedly and the computational complexity seems huge, way over O(n^2) + terms are traversed both bottom-up and top-down, so that's already O(n^2) + canon_point calls itself recursively even on exprs of the same size Q: Is there a good canonicalization algorithm from literature we can use? A purely functional one? *) (* TODO: expr is mixing up int and uint expressions in a very untyped way. How am I supposed to write an expr_to_term function that always produces well-typed terms when types are so badly mixed up? Tempted to roll out a simplified version of expr that only does ints. *) (* This is the pure part of canon_point *) (* TODO: need Tot to state the lemma Nik wanted, but order not easy at all, and I'm not sure it's worth investing time in verifying such a bad algorithm *) let rec canon_point (e:expr) : Dv expr = match e with // Evaluate constants | Plus (Lit a) (Lit b) -> Lit (a + b) | Mult (Lit a) (Lit b) -> Lit (a * b) // Forget about negations | Neg e -> canon_point (Mult (Lit (-1)) e) // Distribute | Mult a (Plus b c) -> let l = canon_point (Mult a b) in let r = canon_point (Mult a c) in canon_point (Plus l r) | Mult (Plus a b) c -> let l = canon_point (Mult a c) in let r = canon_point (Mult b c) in canon_point (Plus l r) // Associate to the left | Mult a (Mult b c) -> let l = canon_point (Mult a b) in let r = canon_point c in canon_point (Mult l r) | Plus a (Plus b c) -> let l = canon_point (Plus a b) in let r = canon_point c in canon_point (Plus l r) | Plus (Plus a b) c -> if O.gt (compare_expr b c) then begin let l = canon_point (Plus a c) in Plus l b end else e | Mult (Mult a b) c -> if O.gt (compare_expr b c) then begin let l = canon_point (Mult a c) in Mult l b end else e | Plus a (Lit 0) -> a | Plus (Lit 0) b -> b | Plus a b -> if O.gt (compare_expr a b) then Plus b a else e | Mult (Lit 0) _ -> Lit 0 | Mult _ (Lit 0) -> Lit 0 | Mult (Lit 1) r -> r | Mult l (Lit 1) -> l | Mult a b -> if O.gt (compare_expr a b) then Mult b a else e // Forget about subtraction | Minus a b -> let r = canon_point (Neg b) in canon_point (Plus a r) | _ -> e (* This is trying to emulate `pointwise canon_point` *) let rec canon_expr (e:expr) : Dv expr = match e with | Atom _ _ | Lit _ -> e | Plus l r -> canon_point (Plus (canon_expr l) (canon_expr r)) | Minus l r -> canon_point (Minus (canon_expr l) (canon_expr r)) | Mult l r -> canon_point (Mult (canon_expr l) (canon_expr r)) | Neg l -> canon_point (Neg (canon_expr l)) | Land l r -> canon_point (Land (canon_expr l) (canon_expr r)) | Lor l r -> canon_point (Lor (canon_expr l) (canon_expr r)) | Lxor l r -> canon_point (Lxor (canon_expr l) (canon_expr r)) | Ladd l r -> canon_point (Ladd (canon_expr l) (canon_expr r)) | Lsub l r -> canon_point (Lsub (canon_expr l) (canon_expr r)) | Shl l r -> canon_point (Shl (canon_expr l) (canon_expr r)) | Shr l r -> canon_point (Shr (canon_expr l) (canon_expr r)) | NatToBv l -> canon_point (NatToBv (canon_expr l)) | Udiv l r -> canon_point (Udiv (canon_expr l) (canon_expr r)) | Umod l r -> canon_point (Umod (canon_expr l) (canon_expr r)) | MulMod l r -> canon_point (MulMod (canon_expr l) (canon_expr r)) (* TODO: stop gap until we have lift from DIV to TAC; actually until we can prove canon_expr in Tot, huh? (see cannon_correct below) *) let canon_expr' (e:expr) : Tot expr = e
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Reflection.V2.Arith.fst.checked", "FStar.Reflection.V2.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Order.fst.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "CanonDeep.fst" }
[ { "abbrev": true, "full_module": "FStar.Order", "short_module": "O" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Reflection.V2.Arith", "short_module": null }, { "abbrev": false, "full_module": "FStar.Reflection.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "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: FStar.Stubs.Reflection.Types.name -> FStar.Stubs.Reflection.Types.term
Prims.Tot
[ "total" ]
[]
[ "FStar.Stubs.Reflection.Types.name", "FStar.Stubs.Reflection.V2.Builtins.pack_ln", "FStar.Stubs.Reflection.V2.Data.Tv_FVar", "FStar.Stubs.Reflection.V2.Builtins.pack_fv", "FStar.Stubs.Reflection.Types.term" ]
[]
false
false
false
true
false
let pack_fv' (n: name) : term =
pack_ln (Tv_FVar (pack_fv n))
false
CanonDeep.fst
CanonDeep.canon_expr'
val canon_expr' (e: expr) : Tot expr
val canon_expr' (e: expr) : Tot expr
let canon_expr' (e:expr) : Tot expr = e
{ "file_name": "examples/native_tactics/CanonDeep.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 39, "end_line": 140, "start_col": 0, "start_line": 140 }
(* Copyright 2008-2018 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 CanonDeep open FStar.Tactics.V2 open FStar.Reflection.V2 open FStar.Reflection.V2.Arith open FStar.Mul module O = FStar.Order (* TODO: The implementation from FStar.Tactics.Canon seems *very* naive: - it repeatedly turns terms into exprs, but that's fixed below - more importantly, a lot of work seems to be done repeatedly and the computational complexity seems huge, way over O(n^2) + terms are traversed both bottom-up and top-down, so that's already O(n^2) + canon_point calls itself recursively even on exprs of the same size Q: Is there a good canonicalization algorithm from literature we can use? A purely functional one? *) (* TODO: expr is mixing up int and uint expressions in a very untyped way. How am I supposed to write an expr_to_term function that always produces well-typed terms when types are so badly mixed up? Tempted to roll out a simplified version of expr that only does ints. *) (* This is the pure part of canon_point *) (* TODO: need Tot to state the lemma Nik wanted, but order not easy at all, and I'm not sure it's worth investing time in verifying such a bad algorithm *) let rec canon_point (e:expr) : Dv expr = match e with // Evaluate constants | Plus (Lit a) (Lit b) -> Lit (a + b) | Mult (Lit a) (Lit b) -> Lit (a * b) // Forget about negations | Neg e -> canon_point (Mult (Lit (-1)) e) // Distribute | Mult a (Plus b c) -> let l = canon_point (Mult a b) in let r = canon_point (Mult a c) in canon_point (Plus l r) | Mult (Plus a b) c -> let l = canon_point (Mult a c) in let r = canon_point (Mult b c) in canon_point (Plus l r) // Associate to the left | Mult a (Mult b c) -> let l = canon_point (Mult a b) in let r = canon_point c in canon_point (Mult l r) | Plus a (Plus b c) -> let l = canon_point (Plus a b) in let r = canon_point c in canon_point (Plus l r) | Plus (Plus a b) c -> if O.gt (compare_expr b c) then begin let l = canon_point (Plus a c) in Plus l b end else e | Mult (Mult a b) c -> if O.gt (compare_expr b c) then begin let l = canon_point (Mult a c) in Mult l b end else e | Plus a (Lit 0) -> a | Plus (Lit 0) b -> b | Plus a b -> if O.gt (compare_expr a b) then Plus b a else e | Mult (Lit 0) _ -> Lit 0 | Mult _ (Lit 0) -> Lit 0 | Mult (Lit 1) r -> r | Mult l (Lit 1) -> l | Mult a b -> if O.gt (compare_expr a b) then Mult b a else e // Forget about subtraction | Minus a b -> let r = canon_point (Neg b) in canon_point (Plus a r) | _ -> e (* This is trying to emulate `pointwise canon_point` *) let rec canon_expr (e:expr) : Dv expr = match e with | Atom _ _ | Lit _ -> e | Plus l r -> canon_point (Plus (canon_expr l) (canon_expr r)) | Minus l r -> canon_point (Minus (canon_expr l) (canon_expr r)) | Mult l r -> canon_point (Mult (canon_expr l) (canon_expr r)) | Neg l -> canon_point (Neg (canon_expr l)) | Land l r -> canon_point (Land (canon_expr l) (canon_expr r)) | Lor l r -> canon_point (Lor (canon_expr l) (canon_expr r)) | Lxor l r -> canon_point (Lxor (canon_expr l) (canon_expr r)) | Ladd l r -> canon_point (Ladd (canon_expr l) (canon_expr r)) | Lsub l r -> canon_point (Lsub (canon_expr l) (canon_expr r)) | Shl l r -> canon_point (Shl (canon_expr l) (canon_expr r)) | Shr l r -> canon_point (Shr (canon_expr l) (canon_expr r)) | NatToBv l -> canon_point (NatToBv (canon_expr l)) | Udiv l r -> canon_point (Udiv (canon_expr l) (canon_expr r)) | Umod l r -> canon_point (Umod (canon_expr l) (canon_expr r)) | MulMod l r -> canon_point (MulMod (canon_expr l) (canon_expr r)) (* TODO: stop gap until we have lift from DIV to TAC; actually until we can prove canon_expr in Tot, huh?
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Reflection.V2.Arith.fst.checked", "FStar.Reflection.V2.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Order.fst.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "CanonDeep.fst" }
[ { "abbrev": true, "full_module": "FStar.Order", "short_module": "O" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Reflection.V2.Arith", "short_module": null }, { "abbrev": false, "full_module": "FStar.Reflection.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "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: FStar.Reflection.V2.Arith.expr -> FStar.Reflection.V2.Arith.expr
Prims.Tot
[ "total" ]
[]
[ "FStar.Reflection.V2.Arith.expr" ]
[]
false
false
false
true
false
let canon_expr' (e: expr) : Tot expr =
e
false
LowParseWriters.fsti
LowParseWriters.cat_spec
val cat_spec (#inv: memory_invariant) (#p: parser) (x: ptr p inv) : Tot (repr_spec unit parse_empty p (fun _ -> True) (fun _ _ vout -> vout == deref_spec x) (fun _ -> False))
val cat_spec (#inv: memory_invariant) (#p: parser) (x: ptr p inv) : Tot (repr_spec unit parse_empty p (fun _ -> True) (fun _ _ vout -> vout == deref_spec x) (fun _ -> False))
let cat_spec (#inv: memory_invariant) (#p: parser) (x: ptr p inv) : Tot (repr_spec unit parse_empty p (fun _ -> True) (fun _ _ vout -> vout == deref_spec x) (fun _ -> False)) = fun _ -> Correct ((), deref_spec x)
{ "file_name": "examples/layeredeffects/LowParseWriters.fsti", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 37, "end_line": 1975, "start_col": 0, "start_line": 1970 }
(* Copyright 2019 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 LowParseWriters include LowParseWriters.LowParse open FStar.HyperStack.ST module HS = FStar.HyperStack module B = LowStar.Buffer module U8 = FStar.UInt8 module U32 = FStar.UInt32 module HST = FStar.HyperStack.ST irreducible let refl_implicit : unit = () open FStar.Tactics.V2 noeq [@erasable] type memory_invariant = { h0: Ghost.erased HS.mem; lwrite: (lwrite: (Ghost.erased B.loc) { B.loc_disjoint lwrite (B.loc_all_regions_from false (HS.get_tip h0)) }); } unfold let memory_invariant_includes (ol ne: memory_invariant) = B.modifies ol.lwrite ol.h0 ne.h0 /\ HS.get_tip ol.h0 `HS.includes` HS.get_tip ne.h0 /\ ol.lwrite `B.loc_includes` ne.lwrite let memory_invariant_includes_trans (l1 l2 l3: memory_invariant) : Lemma (requires (l1 `memory_invariant_includes` l2 /\ l2 `memory_invariant_includes` l3)) (ensures (l1 `memory_invariant_includes` l3)) = () noeq type result (a: Type u#x) : Type u#x = | Correct of a | Error of string let pure_post_err (pre: pure_pre) : Tot Type = unit (* squash pre *) -> GTot Type0 let pure_post' (a: Type) (pre: pure_pre) : Tot Type = (x: a) -> GTot Type0 // (requires pre) (ensures (fun _ -> True)) inline_for_extraction let read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) : Tot (Type u#x) = unit -> Ghost (result a) (requires pre) (ensures (fun res -> match res with | Correct v -> post v | Error _ -> post_err () )) inline_for_extraction val read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) : Tot Type0 inline_for_extraction val mk_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: ( unit -> HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) )) : Tot (read_repr_impl a pre post post_err l spec) inline_for_extraction val extract_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) (spec: read_repr_spec a pre post post_err) (impl: read_repr_impl a pre post post_err l spec) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == spec () )) [@@ commute_nested_matches ] inline_for_extraction noeq type read_repr (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) = | ReadRepr: spec: read_repr_spec a pre post post_err -> impl: read_repr_impl a pre post post_err l spec -> read_repr a pre post post_err l let read_return_spec (a:Type) (x:a) : Tot (read_repr_spec a True (fun res -> res == x) (fun _ -> False)) = fun _ -> Correct x inline_for_extraction val read_return_impl (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr_impl a True (fun res -> res == x) (fun _ -> False) inv (read_return_spec a x)) inline_for_extraction let read_return (a:Type) (x:a) (inv: memory_invariant) : Tot (read_repr a True (fun res -> res == x) (fun _ -> False) inv) = ReadRepr _ (read_return_impl a x inv) // let read_bind_pre // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) // : Tot pure_pre // = pre_f /\ (forall (x: a) . post_f x ==> pre_g x) // let read_bind_post // (a:Type) (b:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) // : Tot (pure_post' b (read_bind_pre a pre_f post_f pre_g)) // = fun y -> // exists x . pre_f /\ post_f x /\ post_g x y // let read_bind_post_err // (a:Type) // (pre_f: pure_pre) (post_f: pure_post' a pre_f) // (post_err_f: pure_post_err pre_f) // (pre_g: (x:a) -> pure_pre) // (post_err_g: (x: a) -> pure_post_err (pre_g x)) // : Tot (pure_post_err (read_bind_pre a pre_f post_f pre_g)) // = fun _ -> // pre_f /\ (post_err_f () \/ (exists x . post_f x /\ post_err_g x ())) let read_bind_spec (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_spec: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) : Tot (read_repr_spec b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) ) = fun _ -> match f_bind_spec () with | Correct a -> g a () | Error e -> Error e inline_for_extraction val read_bind_impl (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) (f_bind_impl: read_repr_spec a pre_f post_f post_err_f) (g:(x:a -> read_repr_spec b (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : read_repr_impl a pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> read_repr_impl b (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (read_repr_impl b _ _ _ l (read_bind_spec a b pre_f post_f post_err_f pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let read_bind (a:Type) (b:Type) (pre_f: pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: (x:a) -> pure_pre) (post_g: (x:a) -> pure_post' b (pre_g x)) (post_err_g: (x: a) -> pure_post_err (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_bind : read_repr a pre_f post_f post_err_f l_f) (g : (x: a -> read_repr b (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (read_repr b (pre_f /\ (forall (x: a) . post_f x ==> pre_g x)) //(read_bind_pre a pre_f post_f pre_g) (fun y -> exists (x: a) . pre_f /\ post_f x /\ post_g x y) // (read_bind_post a b pre_f post_f pre_g post_g) (fun _ -> pre_f /\ (post_err_f () \/ (exists (x: a) . post_f x /\ post_err_g x ()))) // (read_bind_post_err a pre_f post_f post_err_f pre_g post_err_g) l_g ) = ReadRepr _ (read_bind_impl a b pre_f post_f post_err_f pre_g post_g post_err_g (ReadRepr?.spec f_bind) (fun x -> ReadRepr?.spec (g x)) l_g (ReadRepr?.impl f_bind) (fun x -> ReadRepr?.impl (g x)) ) unfold let read_subcomp_spec_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') : GTot Type0 = (pre' ==> pre) /\ (forall x . (pre' /\ post x) ==> post' x) /\ ((pre' /\ post_err ()) ==> post_err' ()) unfold let read_subcomp_cond (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ read_subcomp_spec_cond a pre post post_err pre' post' post_err' let read_subcomp_spec (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (f_subcomp:read_repr_spec a pre post post_err) : Pure (read_repr_spec a pre' post' post_err') (requires (read_subcomp_spec_cond a pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val read_subcomp_impl (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l:memory_invariant) (l' : memory_invariant) (f_subcomp_spec:read_repr_spec a pre post post_err) (f_subcomp:read_repr_impl a pre post post_err l f_subcomp_spec) (sq: squash (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) : Tot (read_repr_impl a pre' post' post_err' l' (read_subcomp_spec a pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let read_subcomp (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l:memory_invariant) (pre': pure_pre) (post': pure_post' a pre') (post_err': pure_post_err pre') (l' : memory_invariant) (f_subcomp:read_repr a pre post post_err l) : Pure (read_repr a pre' post' post_err' l') (requires (read_subcomp_cond a pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = ReadRepr (read_subcomp_spec a pre post post_err pre' post' post_err' (ReadRepr?.spec f_subcomp)) (read_subcomp_impl a pre post post_err pre' post' post_err' l l' (ReadRepr?.spec f_subcomp) (ReadRepr?.impl f_subcomp) () ) inline_for_extraction let read_if_then_else (a:Type) (pre_f:pure_pre) (post_f: pure_post' a pre_f) (post_err_f: pure_post_err pre_f) ([@@@ refl_implicit] l_f:memory_invariant) (pre_g: pure_pre) (post_g: pure_post' a pre_g) (post_err_g: pure_post_err pre_g) ([@@@ refl_implicit] l_g:memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) (f_ifthenelse:read_repr a pre_f post_f post_err_f l_f) (g:read_repr a pre_g post_g post_err_g l_g) (p:bool) : Tot Type = read_repr a ((p ==> pre_f) /\ ((~ p) ==> pre_g)) // (read_if_then_else_pre pre_f pre_g p) (fun x -> (p ==> post_f x) /\ ((~ p) ==> post_g x)) // (read_if_then_else_post a pre_f pre_g post_f post_g p) (fun _ -> (p ==> post_err_f ()) /\ ((~ p) ==> post_err_g ())) // (read_if_then_else_post_err pre_f pre_g post_err_f post_err_g p) l_g reifiable reflectable total effect { ERead (a:Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (_:memory_invariant) with {repr = read_repr; return = read_return; bind = read_bind; subcomp = read_subcomp; if_then_else = read_if_then_else} } effect Read (a:Type) (pre: pure_pre) (post: pure_post' a pre) (inv: memory_invariant) = ERead a pre post (fun _ -> False) inv let lift_pure_read_spec (a:Type) (wp:pure_wp a) (f_pure_spec:unit -> PURE a wp) : Tot (read_repr_spec a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) ) = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun () -> Correct (f_pure_spec ()) inline_for_extraction val lift_pure_read_impl (a:Type) (wp:pure_wp a) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (read_repr_impl a _ _ _ l (lift_pure_read_spec a wp f_pure_spec_for_impl)) open FStar.Monotonic.Pure inline_for_extraction let lift_pure_read (a:Type) (wp:pure_wp a) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (read_repr a (wp (fun _ -> True)) // (lift_pure_read_pre a wp) (fun x -> ~ (wp (fun x' -> ~ (x == x')))) // (lift_pure_read_post a wp) (fun _ -> False) // (lift_pure_read_post_err a wp)) l ) = elim_pure_wp_monotonicity_forall (); ReadRepr _ (lift_pure_read_impl a wp f_pure l) sub_effect PURE ~> ERead = lift_pure_read let destr_read_repr_spec (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_spec a pre post post_err) = ReadRepr?.spec (reify (r ())) inline_for_extraction let destr_read_repr_impl (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : Tot (read_repr_impl a pre post post_err l (destr_read_repr_spec a pre post post_err l r)) = ReadRepr?.impl (reify (r ())) inline_for_extraction let reify_read (a:Type u#x) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (l: memory_invariant) ($r: unit -> ERead a pre post post_err l) : HST.Stack (result a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ pre )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ res == destr_read_repr_spec _ _ _ _ _ r () )) = extract_read_repr_impl _ _ _ _ _ _ (destr_read_repr_impl _ _ _ _ _ r) 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 let is_eq (t:term) : Tac (option (term & term)) = match term_as_formula t with | Comp (Eq _) l r -> Some (l, r) | _ -> None // (match term_as_formula' t with // | Comp (Eq _) l r -> Some (l, r) // | _ -> None) let rec pick_next (fuel:nat) : Tac bool = if fuel = 0 then false else match goals () with | [] -> false | hd::tl -> (match is_eq (goal_type hd) with | Some (l, r) -> let b1 = is_uvar l in let b2 = is_uvar r in if b1 && b2 then begin later (); pick_next (fuel - 1) end else true | None -> later (); pick_next (fuel - 1)) let rec try_rewrite_equality (x:term) (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin match term_as_formula' x_t.sort with | Comp (Eq _) y _ -> if term_eq x y then rewrite (binder_to_binding x_t) else try_rewrite_equality x bs | _ -> try_rewrite_equality x bs end let rec rewrite_all_context_equalities (bs:binders) : Tac unit = match bs with | [] -> () | x_t::bs -> begin (try rewrite (binder_to_binding x_t) with | _ -> ()); rewrite_all_context_equalities bs end let rewrite_eqs_from_context_non_sq () : Tac unit = rewrite_all_context_equalities (List.Tot.map binding_to_binder (cur_vars ())) let rewrite_eqs_from_context () : Tac unit = rewrite_eqs_from_context (); rewrite_eqs_from_context_non_sq () [@@ resolve_implicits; refl_implicit] let rec tac () : Tac unit = if List.length (goals ()) = 0 then () else if pick_next (FStar.List.length (goals ())) then begin or_else (fun _ -> rewrite_eqs_from_context (); norm []; trefl ()) (fun _ -> assumption ()); tac () end else begin rewrite_eqs_from_context (); norm []; trefl (); tac () end inline_for_extraction let test_read_if (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = if f () then false else false inline_for_extraction let test_read1 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in false let test_read2 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in false inline_for_extraction let test_read3 (inv: memory_invariant) (f: unit -> Read bool (True) (fun _ -> True) inv) : Read bool (True) (fun _ -> True) inv = let x = f () in let x = f () in let x = f () in false let failwith_spec (a: Type) (s: string) : Tot (read_repr_spec a True (fun _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val failwith_impl (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr_impl a True (fun _ -> False) (fun _ -> True) inv (failwith_spec a s)) inline_for_extraction let failwith_repr (a: Type) (inv: memory_invariant) (s: string) : Tot (read_repr a True (fun _ -> False) (fun _ -> True) inv) = ReadRepr _ (failwith_impl a inv s) inline_for_extraction let failwith (#a: Type) (#inv: memory_invariant) (s: string) : ERead a True (fun _ -> False) (fun _ -> True) inv = ERead?.reflect (failwith_repr a inv s) let buffer_index_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_spec t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) (fun _ -> False) ) = fun _ -> let j = U32.v i in Correct (Seq.index (B.as_seq inv.h0 b) j) inline_for_extraction val buffer_index_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_index_spec inv b i)) inline_for_extraction let buffer_index (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) : Read t ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i < B.length b ) (fun res -> U32.v i < B.length b /\ res == Seq.index (B.as_seq inv.h0 b) (U32.v i) ) inv = ERead?.reflect (ReadRepr _ (buffer_index_impl inv b i)) let buffer_sub_spec (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_spec (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) (fun _ -> False) ) = fun _ -> Correct (B.gsub b i len) inline_for_extraction val buffer_sub_impl (#t: Type) (inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Tot (read_repr_impl _ _ _ _ inv (buffer_sub_spec inv b i len)) inline_for_extraction let buffer_sub (#t: Type) (#inv: memory_invariant) (b: B.buffer t) (i: U32.t) (len: Ghost.erased U32.t) : Read (B.buffer t) ( B.live inv.h0 b /\ B.loc_buffer b `B.loc_disjoint` inv.lwrite /\ U32.v i + U32.v len <= B.length b ) (fun res -> U32.v i + U32.v len <= B.length b /\ res == B.gsub b i len /\ B.loc_buffer res `B.loc_disjoint` inv.lwrite ) inv = ERead?.reflect (ReadRepr _ (buffer_sub_impl inv b i len)) inline_for_extraction val rptr: Type0 val valid_rptr (p: parser) : memory_invariant -> rptr -> GTot Type0 inline_for_extraction let ptr (p: parser) (inv: memory_invariant) = (x: rptr { valid_rptr p inv x }) val deref_spec (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : GTot (Parser?.t p) inline_for_extraction val mk_ptr (p: parser) (inv: memory_invariant) (b: B.buffer u8) (len: U32.t { len == B.len b }) : Pure (ptr p inv) (requires (valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b)) (ensures (fun res -> deref_spec res == buffer_contents p inv.h0 b)) inline_for_extraction val buffer_of_ptr (#p: parser) (#inv: memory_invariant) (x: ptr p inv) : Tot (bl: (B.buffer u8 & U32.t) { let (b, len) = bl in B.len b == len /\ valid_buffer p inv.h0 b /\ inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ deref_spec x == buffer_contents p inv.h0 b }) val valid_rptr_frame (#p: parser) (#inv: memory_invariant) (x: ptr p inv) (inv' : memory_invariant) : Lemma (requires ( inv `memory_invariant_includes` inv' )) (ensures ( valid_rptr p inv' x /\ deref_spec #p #inv' x == deref_spec #p #inv x )) [SMTPatOr [ [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv x)]; [SMTPat (inv `memory_invariant_includes` inv'); SMTPat (valid_rptr p inv' x)]; ]] inline_for_extraction val deref_impl (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr_impl (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv (fun _ -> Correct (deref_spec x))) inline_for_extraction let deref_repr (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Tot (read_repr (Parser?.t p) True (fun res -> res == deref_spec x) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (deref_spec x)) (deref_impl r x) inline_for_extraction let deref (#p: parser) (#inv: memory_invariant) (r: leaf_reader p) (x: ptr p inv) : Read (Parser?.t p) True (fun res -> res == deref_spec x) inv = Read?.reflect (deref_repr r x) val access_spec (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (g: gaccessor p1 p2 lens) (x: ptr p1 inv) : Ghost (ptr p2 inv) (requires (lens.clens_cond (deref_spec x))) (ensures (fun res -> deref_spec res == lens.clens_get (deref_spec x))) inline_for_extraction val access_impl (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr_impl (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv (fun _ -> Correct (access_spec g x))) inline_for_extraction let access_repr (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Tot (read_repr (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) (fun _ -> False) inv) = ReadRepr (fun _ -> Correct (access_spec g x)) (access_impl a x) inline_for_extraction let access (#p1 #p2: parser) (#lens: clens (Parser?.t p1) (Parser?.t p2)) (#inv: memory_invariant) (#g: gaccessor p1 p2 lens) (a: accessor g) (x: ptr p1 inv) : Read (ptr p2 inv) (lens.clens_cond (deref_spec x)) (fun res -> lens.clens_cond (deref_spec x) /\ deref_spec res == lens.clens_get (deref_spec x)) inv = Read?.reflect (access_repr a x) // unfold let validate_pre (inv: memory_invariant) (b: B.buffer U8.t) : Tot pure_pre = inv.lwrite `B.loc_disjoint` B.loc_buffer b /\ B.live inv.h0 b // unfold let validate_post (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post' (ptr p inv & U32.t) (validate_pre inv b)) = fun (x, pos) -> valid_pos p inv.h0 b 0ul pos /\ deref_spec x == contents p inv.h0 b 0ul pos // unfold let validate_post_err (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (pure_post_err (validate_pre inv b)) = fun _ -> forall pos . ~ (valid_pos p inv.h0 b 0ul pos) val validate_spec (p: parser) (inv: memory_invariant) (b: B.buffer U8.t) : Tot (read_repr_spec (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b)) inline_for_extraction val validate_impl (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr_impl _ _ _ _ inv (validate_spec p inv b)) inline_for_extraction let validate_repr (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : Tot (read_repr (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv) = ReadRepr _ (validate_impl v inv b len) inline_for_extraction let validate (#p: parser) (v: validator p) (inv: memory_invariant) (b: B.buffer U8.t) (len: U32.t { B.len b == len }) : ERead (ptr p inv & U32.t) (validate_pre inv b) (validate_post p inv b) (validate_post_err p inv b) inv = ERead?.reflect (validate_repr v inv b len) let pre_t (rin: parser) : Tot Type = Parser?.t rin -> GTot Type0 let post_t (a: Type) (rin: parser) (rout: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> (res: a) -> Parser?.t rout -> GTot Type0 let post_err_t (rin: parser) (pre: pre_t rin) : Tot Type = (x: Parser?.t rin (* { pre x } *) ) -> GTot Type0 inline_for_extraction let repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) : Tot (Type u#x) = (v_in: Parser?.t r_in) -> Ghost (result (a & Parser?.t r_out)) (requires (pre v_in)) (ensures (fun res -> match res with | Correct (v, v_out) -> post v_in v v_out /\ size r_in v_in <= size r_out v_out | Error _ -> post_err v_in )) noeq type iresult (a: Type u#x) : Type u#x = | ICorrect: (res: a) -> (new_pos : U32.t) -> iresult a | IError of string | IOverflow unfold let repr_impl_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (pos1: U32.t { U32.v pos1 <= B.length b }) (h: HS.mem) (res: iresult a) (h' : HS.mem) : GTot Type0 = valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ) let buffer_offset (#t: Type) (b: B.buffer t) : Tot Type0 = pos1: U32.t { U32.v pos1 <= B.length b } inline_for_extraction val repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) : Tot Type0 inline_for_extraction val mk_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: ( (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) )) : Tot (repr_impl a r_in r_out pre post post_err l spec) inline_for_extraction val extract_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) (len: U32.t { len == B.len b }) (pos1: buffer_offset b) : HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> repr_impl_post a r_in r_out pre post post_err l spec b pos1 h res h' )) [@@ commute_nested_matches ] inline_for_extraction noeq type repr (a: Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) = | Repr: spec: repr_spec a r_in r_out pre post post_err -> impl: repr_impl a r_in r_out pre post post_err l spec -> repr a r_in r_out pre post post_err l let return_spec (a:Type) (x:a) (r: parser) : Tot (repr_spec a r r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False)) = fun c -> Correct (x, c) inline_for_extraction val return_impl (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr_impl a (r) r _ _ _ l (return_spec a x r)) inline_for_extraction let returnc (a:Type) (x:a) (r: parser) (l: memory_invariant) : Tot (repr a (r) r (fun _ -> True) (fun v_in x' v_out -> x' == x /\ v_out == v_in) (fun _ -> False) l) = Repr (return_spec a x r) (return_impl a x r l) let bind_spec (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_spec:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) : Tot (repr_spec b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) ) = fun c -> match f_bind_spec c with | Correct (x, cf) -> g x cf | Error e -> Error e inline_for_extraction val bind_impl (a:Type) (b:Type) (r_in_f:parser) (r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) (r_out_g: parser) (pre_g: (x:a) -> pre_t (r_out_f)) (post_g: (x:a) -> post_t b (r_out_f) r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t (r_out_f) (pre_g x)) (f_bind_impl:repr_spec a r_in_f r_out_f pre_f post_f post_err_f) (g:(x:a -> repr_spec b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x))) (l: memory_invariant) (f' : repr_impl a r_in_f r_out_f pre_f post_f post_err_f l f_bind_impl) (g' : (x: a -> repr_impl b (r_out_f) r_out_g (pre_g x) (post_g x) (post_err_g x) l (g x))) : Tot (repr_impl b r_in_f r_out_g _ _ _ l (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g f_bind_impl g)) inline_for_extraction let bind (a:Type) (b:Type) (r_in_f:parser) ([@@@ refl_implicit] r_out_f: parser) (pre_f: pre_t r_in_f) (post_f: post_t a r_in_f r_out_f pre_f) (post_err_f: post_err_t r_in_f pre_f) ([@@@ refl_implicit] l_f: memory_invariant) ([@@@ refl_implicit] r_in_g:parser) (r_out_g: parser) (pre_g: (x:a) -> pre_t r_in_g) (post_g: (x:a) -> post_t b r_in_g r_out_g (pre_g x)) (post_err_g: (x:a) -> post_err_t r_in_g (pre_g x)) ([@@@ refl_implicit] l_g: memory_invariant) ([@@@ refl_implicit] pr:squash (l_f == l_g)) ([@@@ refl_implicit] pr':squash (r_out_f == r_in_g)) (f_bind : repr a r_in_f r_out_f pre_f post_f post_err_f l_f) (g : (x: a -> repr b (r_in_g) r_out_g (pre_g x) (post_g x) (post_err_g x) l_g)) : Tot (repr b r_in_f r_out_g (fun v_in -> pre_f v_in /\ (forall (x: a) v_out . post_f v_in x v_out ==> pre_g x v_out)) // (bind_pre a r_in_f r_out_f pre_f post_f pre_g) (fun v_in y v_out -> exists x v_out_f . pre_f v_in /\ post_f v_in x v_out_f /\ post_g x v_out_f y v_out) // (bind_post a b r_in_f r_out_f pre_f post_f r_out_g pre_g post_g) (fun v_in -> pre_f v_in /\ ( post_err_f v_in \/ ( exists x v_out_f . post_f v_in x v_out_f /\ post_err_g x v_out_f ))) // (bind_post_err a r_in_f r_out_f pre_f post_f post_err_f pre_g post_err_g)) l_g ) = Repr (bind_spec a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x))) (bind_impl a b r_in_f r_out_f pre_f post_f post_err_f r_out_g pre_g post_g post_err_g (Repr?.spec f_bind) (fun x -> Repr?.spec (g x)) l_g (Repr?.impl f_bind) (fun x -> Repr?.impl (g x))) unfold let subcomp_spec_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') : GTot Type0 = (forall v_in . pre' v_in ==> pre v_in) /\ (forall v_in x v_out . (pre' v_in /\ post v_in x v_out) ==> post' v_in x v_out) /\ (forall v_in . (pre' v_in /\ post_err v_in) ==> post_err' v_in) unfold let subcomp_cond (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l: memory_invariant) (l' : memory_invariant) : GTot Type0 = l `memory_invariant_includes` l' /\ subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err' let subcomp_spec (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (f_subcomp:repr_spec a r_in r_out pre post post_err) : Pure (repr_spec a r_in r_out pre' post' post_err') (requires (subcomp_spec_cond a r_in r_out pre post post_err pre' post' post_err')) (ensures (fun _ -> True)) = (fun x -> f_subcomp x) inline_for_extraction val subcomp_impl (a:Type) (r_in:parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l:memory_invariant) (r_in':parser) (r_out': parser) (pre': pre_t r_in) (post': post_t a r_in r_out pre') (post_err': post_err_t r_in pre') (l' : memory_invariant) (f_subcomp_spec:repr_spec a r_in r_out pre post post_err) (f_subcomp:repr_impl a r_in r_out pre post post_err l f_subcomp_spec) (sq: squash (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) : Tot (repr_impl a r_in r_out pre' post' post_err' l' (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' f_subcomp_spec)) inline_for_extraction let subcomp (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre': pre_t r_in') (post': post_t a r_in' r_out' pre') (post_err': post_err_t r_in' pre') ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) (f_subcomp:repr a r_in r_out pre post post_err l) : Pure (repr a r_in' r_out' pre' post' post_err' l') (requires (subcomp_cond a r_in r_out pre post post_err pre' post' post_err' l l')) (ensures (fun _ -> True)) = Repr (subcomp_spec a r_in r_out pre post post_err pre' post' post_err' (Repr?.spec f_subcomp)) (subcomp_impl a r_in r_out pre post post_err l r_in' r_out' pre' post' post_err' l' (Repr?.spec f_subcomp) (Repr?.impl f_subcomp) () ) let if_then_else (a:Type) ([@@@refl_implicit] r_in:parser) ([@@@ refl_implicit] r_out: parser) (pre_f: pre_t r_in) (post_f: post_t a r_in r_out pre_f) (post_err_f: post_err_t r_in pre_f) ([@@@ refl_implicit] l:memory_invariant) ([@@@refl_implicit] r_in':parser) ([@@@ refl_implicit] r_out': parser) (pre_g: pre_t r_in') (post_g: post_t a r_in' r_out' pre_g) (post_err_g: post_err_t r_in' pre_g) ([@@@ refl_implicit] l' : memory_invariant) ([@@@ refl_implicit] pr1:squash (r_in == r_in')) ([@@@ refl_implicit] pr2:squash (r_out == r_out')) ([@@@ refl_implicit] pr3:squash (l == l')) (f_ifthenelse:repr a r_in r_out pre_f post_f post_err_f l) (g:repr a r_in' r_out' pre_g post_g post_err_g l') (p:bool) : Tot Type = repr a r_in r_out (fun v_in -> (p ==> pre_f v_in) /\ ((~ p) ==> pre_g v_in)) // (if_then_else_pre r_in pre_f pre_g p) (fun v_in x v_out -> (p ==> post_f v_in x v_out) /\ ((~ p) ==> post_g v_in x v_out)) // (if_then_else_post a r_in r_out pre_f pre_g post_f post_g p) (fun v_in -> (p ==> post_err_f v_in) /\ ((~ p) ==> post_err_g v_in)) // (if_then_else_post_err r_in pre_f pre_g post_err_f post_err_g p) l reifiable reflectable total effect { EWrite (a:Type) (pin:parser) (pout:parser) (pre:pre_t pin) (post:post_t a pin pout pre) (post_err:post_err_t pin pre) (_:memory_invariant) with {repr = repr; return = returnc; bind = bind; subcomp = subcomp; if_then_else = if_then_else} } effect Write (a:Type) (pin: parser) (pout: (parser)) (pre: pre_t pin) (post: post_t a pin pout pre) (inv: memory_invariant) = EWrite a pin pout pre post (fun _ -> False) inv (* unfold let lift_pure_pre (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (pre_t r) = fun st -> wp (fun _ -> True) unfold let lift_pure_post (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) : Tot (post_t a r (fun _ -> r) (lift_pure_pre a wp r)) = fun st x st' -> ~ (wp (fun x' -> ~ (st == st' /\ x == x'))) let lift_pure_spec (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec:unit -> PURE a wp) : Tot (repr_spec a r (fun _ -> r) (lift_pure_pre a wp r) (lift_pure_post a wp r)) = fun v -> (| f_pure_spec (), v |) inline_for_extraction val lift_pure_impl (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (f_pure_spec_for_impl:unit -> PURE a wp) (l: memory_invariant) : Tot (repr_impl a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l (lift_pure_spec a wp r f_pure_spec_for_impl)) inline_for_extraction let lift_pure (a:Type) (wp:pure_wp a { pure_wp_mono a wp }) (r:parser) (l: memory_invariant) (f_pure:unit -> PURE a wp) : Tot (repr a r (fun _ -> r) (fun v_in -> lift_pure_pre a wp r v_in) (fun v_in x v_out -> lift_pure_post a wp r v_in x v_out) l) = (| lift_pure_spec a wp r f_pure, lift_pure_impl a wp r f_pure l |) sub_effect PURE ~> Write = lift_pure *) let lift_read_spec (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_spec a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) ) = fun st -> match ReadRepr?.spec f_read_spec () with | Correct res -> Correct (res, st) | Error e -> Error e inline_for_extraction val lift_read_impl (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr_impl a r (r) _ _ _ inv (lift_read_spec a pre post post_err inv r f_read_spec)) inline_for_extraction let lift_read (a: Type) (pre: pure_pre) (post: pure_post' a pre) (post_err: pure_post_err pre) (inv: memory_invariant) (r: parser) (f_read_spec: read_repr a pre post post_err inv) : Tot (repr a r (r) (fun _ -> pre) // (lift_read_pre pre r) (fun st x st' -> st == st' /\ post x) // (lift_read_post a pre post r) (fun _ -> post_err ()) // (lift_read_post_err pre post_err r)) inv ) = Repr (lift_read_spec a pre post post_err inv r f_read_spec) (lift_read_impl a pre post post_err inv r f_read_spec) sub_effect ERead ~> EWrite = lift_read let wfailwith_spec (a: Type) (rin rout: parser) (s: string) : Tot (repr_spec a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True)) = fun _ -> Error s inline_for_extraction val wfailwith_impl (a: Type) (inv: memory_invariant) (rin rout: parser) (s: string) : Tot (repr_impl a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv (wfailwith_spec a rin rout s)) inline_for_extraction let wfailwith (#a: Type) (#inv: memory_invariant) (#rin #rout: parser) (s: string) : EWrite a rin rout (fun _ -> True) (fun _ _ _ -> False) (fun _ -> True) inv = EWrite?.reflect (Repr _ (wfailwith_impl a inv rin rout s)) // unfold let destr_repr_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre post post_err) = Repr?.spec (reify (f_destr_spec ())) inline_for_extraction let destr_repr_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre post post_err l (destr_repr_spec a r_in r_out pre post post_err l f_destr_spec)) = Repr?.impl (reify (f_destr_spec ())) inline_for_extraction let extract_t (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot Type = (b: B.buffer u8 { l.lwrite `B.loc_includes` B.loc_buffer b }) -> (len: U32.t { len == B.len b }) -> (pos1: buffer_offset b) -> HST.Stack (iresult a) (requires (fun h -> B.modifies l.lwrite l.h0 h /\ HS.get_tip l.h0 `HS.includes` HS.get_tip h /\ valid_pos r_in h b 0ul pos1 /\ pre (contents r_in h b 0ul pos1) )) (ensures (fun h res h' -> valid_pos r_in h b 0ul pos1 /\ B.modifies (B.loc_buffer b) h h' /\ ( let v_in = contents r_in h b 0ul pos1 in pre v_in /\ begin match destr_repr_spec _ _ _ _ _ _ _ f_destr_spec v_in, res with | Correct (v, v_out), ICorrect v' pos2 -> U32.v pos1 <= U32.v pos2 /\ valid_pos (r_out) h' b 0ul pos2 /\ v' == v /\ v_out == contents (r_out) h' b 0ul pos2 | Correct (v, v_out), IOverflow -> size (r_out) v_out > B.length b | Error s, IError s' -> s == s' | Error _, IOverflow -> (* overflow happened in implementation before specification could reach error *) True | _ -> False end ))) inline_for_extraction let extract (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) : Tot (extract_t l f_destr_spec) = extract_repr_impl _ _ _ _ _ _ _ _ (destr_repr_impl _ _ _ _ _ _ _ f_destr_spec) inline_for_extraction let wrap_extracted_impl (#a:Type u#x) (#r_in: parser) (#r_out: parser) (#pre: pre_t r_in) (#post: post_t a r_in r_out pre) (#post_err: post_err_t r_in pre) (l: memory_invariant) ($f_destr_spec: unit -> EWrite a r_in r_out pre post post_err l) (e: extract_t l f_destr_spec) : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) ( mk_repr_impl a r_in r_out _ _ _ _ (destr_repr_spec _ _ _ _ _ _ _ f_destr_spec) (fun b len pos1 -> e b len pos1) )) inline_for_extraction unfold let mk_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (spec: repr_spec a r_in r_out pre post post_err) (impl: repr_impl a r_in r_out pre post post_err l spec) () : EWrite a r_in r_out pre post post_err l = EWrite?.reflect (Repr spec impl) let get_state_spec (p: parser) : Tot (repr_spec (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) (fun _ -> False)) = fun x -> Correct (Ghost.hide x, x) inline_for_extraction val get_state_impl (inv: memory_invariant) (p: parser) : Tot (repr_impl _ _ _ _ _ _ inv (get_state_spec p)) inline_for_extraction let get_state (#inv: memory_invariant) (#p: parser) () : Write (Ghost.erased (Parser?.t p)) p p (fun _ -> True) (fun x y z -> x == Ghost.reveal y /\ x == z) inv = EWrite?.reflect (Repr _ (get_state_impl inv p)) let frame_out (a: Type) (frame: parser) (p: (parser)) : Tot parser = frame `parse_pair` p unfold let frame_pre (frame: parser) (pre: pre_t parse_empty) : Tot (pre_t frame) = fun _ -> pre () unfold let frame_post (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) : Tot (post_t a frame (frame_out a frame p) (frame_pre frame pre)) = fun v_in v (v_in', v_out) -> v_in == v_in' /\ post () v v_out unfold let frame_post_err (frame: parser) (pre: pre_t parse_empty) (post_err: post_err_t parse_empty pre) : Tot (post_err_t frame (frame_pre frame pre)) = fun _ -> post_err () let frame_spec (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_spec a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err)) = fun fr -> match destr_repr_spec a parse_empty p pre post post_err l inner () with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame_impl (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (repr_impl a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner)) inline_for_extraction let frame_repr (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) (inner: unit -> EWrite a parse_empty p pre post post_err l) : Tot (unit -> EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l) = mk_repr a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l (frame_spec a frame pre p post post_err l inner) (frame_impl a frame pre p post post_err l inner) inline_for_extraction let frame (a: Type) (frame: parser) (pre: pre_t parse_empty) (p: parser) (post: post_t a parse_empty p pre) (post_err: post_err_t parse_empty pre) (l: memory_invariant) ($inner: unit -> EWrite a parse_empty p pre post post_err l) : EWrite a frame (frame_out a frame p) (frame_pre frame pre) (frame_post a frame pre p post) (frame_post_err frame pre post_err) l = frame_repr a frame pre p post post_err l inner () let elem_writer_spec (p: parser) (x: Parser?.t p) : Tot (repr_spec unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False)) = fun _ -> Correct ((), x) inline_for_extraction val elem_writer_impl (#p: parser) (w: leaf_writer p) (l: memory_invariant) (x: Parser?.t p) : Tot (repr_impl unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x)) inline_for_extraction let start (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) l = mk_repr unit parse_empty (p) (fun _ -> True) (fun _ _ y -> y == x) (fun _ -> False) l (elem_writer_spec p x) (elem_writer_impl w l x) () #push-options "--z3rlimit 64" #restart-solver inline_for_extraction let append (#fr: parser) (#p: parser) (w: leaf_writer p) (#l: memory_invariant) (x: Parser?.t p) : Write unit fr (fr `parse_pair` p) (fun _ -> True) (fun w _ (w', x') -> w' == w /\ x' == x) l = frame unit fr (fun _ -> True) (p) (fun _ _ x' -> x' == x) (fun _ -> False) l (fun _ -> start w x) #pop-options unfold let recast_writer_post (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_t a r_in r_out pre) = fun v_in v v_out -> pre v_in /\ destr_repr_spec a r_in r_out pre post post_err l f v_in == Correct (v, v_out) /\ post v_in v v_out unfold let recast_writer_post_err (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (post_err_t r_in pre) = fun v_in -> pre v_in /\ Error? (destr_repr_spec a r_in r_out pre post post_err l f v_in) /\ post_err v_in let recast_writer_spec (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_spec a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f)) = fun v_in -> destr_repr_spec a r_in r_out pre post post_err l f v_in inline_for_extraction val recast_writer_impl (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (repr_impl a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f)) inline_for_extraction let recast_writer_repr (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) (f: unit -> EWrite a r_in r_out pre post post_err l) : Tot (unit -> EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l) = mk_repr a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l (recast_writer_spec a r_in r_out pre post post_err l f) (recast_writer_impl a r_in r_out pre post post_err l f) inline_for_extraction let recast_writer (a:Type u#x) (r_in: parser) (r_out: parser) (pre: pre_t r_in) (post: post_t a r_in r_out pre) (post_err: post_err_t r_in pre) (l: memory_invariant) ($f: unit -> EWrite a r_in r_out pre post post_err l) : EWrite a r_in r_out pre (recast_writer_post a r_in r_out pre post post_err l f) (recast_writer_post_err a r_in r_out pre post post_err l f) l = recast_writer_repr a r_in r_out pre post post_err l f () inline_for_extraction let frame' (a: Type) (fr: parser) (p: parser) (l: memory_invariant) (f: unit -> EWrite a parse_empty p (fun _ -> True) (fun _ _ _ -> True) (fun _ -> True) l ) : EWrite a fr (fr `parse_pair` p) (fun _ -> True) (fun vin v (vin', vout) -> vin' == vin /\ destr_repr_spec _ _ _ _ _ _ _ f () == Correct (v, vout) ) (fun vin -> Error? (destr_repr_spec _ _ _ _ _ _ _ f ()) ) l = frame _ _ _ _ _ _ _ (fun _ -> recast_writer _ _ _ _ _ _ _ f) unfold let frame2_pre (frame: parser) (ppre: parser) (pre: pre_t ppre) : Tot (pre_t (frame `parse_pair` ppre)) = fun (_, x) -> pre x unfold let frame2_post (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) : Tot (post_t a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre)) = fun (v_frame, v_in) v (v_frame', v_out) -> v_frame == v_frame' /\ post v_in v v_out unfold let frame2_post_err (frame: parser) (ppre: parser) (pre: pre_t ppre) (post_err: post_err_t ppre pre) : Tot (post_err_t (frame `parse_pair` ppre) (frame2_pre frame ppre pre)) = fun (_, x) -> post_err x let frame2_spec (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_spec a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err)) = fun (fr, w_in) -> match destr_repr_spec a ppre p pre post post_err l inner w_in with | Correct (v, w) -> Correct (v, (fr, w)) | Error e -> Error e inline_for_extraction val frame2_impl (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (repr_impl a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner)) inline_for_extraction let frame2_repr (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) (inner: unit -> EWrite a ppre p pre post post_err l) : Tot (unit -> EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l) = mk_repr a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l (frame2_spec a frame ppre pre p post post_err l inner) (frame2_impl a frame ppre pre p post post_err l inner) inline_for_extraction let frame2 (a: Type) (frame: parser) (ppre: parser) (pre: pre_t ppre) (p: parser) (post: post_t a ppre p pre) (post_err: post_err_t ppre pre) (l: memory_invariant) ($inner: unit -> EWrite a ppre p pre post post_err l) : EWrite a (frame `parse_pair` ppre) (frame_out a frame p) (frame2_pre frame ppre pre) (frame2_post a frame ppre pre p post) (frame2_post_err frame ppre pre post_err) l = frame2_repr a frame ppre pre p post post_err l inner () noeq [@erasable] // very important, otherwise KaRaMeL will fail with argument typing type valid_rewrite_t (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) = { valid_rewrite_valid: (h: HS.mem) -> (b: B.buffer U8.t) -> (pos: U32.t) -> (pos' : U32.t) -> Lemma (requires ( valid_pos p1 h b pos pos' /\ precond (contents p1 h b pos pos') )) (ensures ( valid_pos p1 h b pos pos' /\ ( let x = contents p1 h b pos pos' in precond x /\ valid_pos p2 h b pos pos' /\ contents p2 h b pos pos' == f x ))); valid_rewrite_size: (x: Parser?.t p1 { precond x }) -> Lemma (size p1 x == size p2 (f x)) } let valid_rewrite_implies (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (precond' : pre_t p1) (f' : (x: Parser?.t p1 { precond' x }) -> GTot (Parser?.t p2)) : Pure (valid_rewrite_t p1 p2 precond' f') (requires ( (forall (x: Parser?.t p1) . precond' x ==> precond x) /\ (forall (x: Parser?.t p1 { precond' x }) . f' x == f x) )) (ensures (fun _ -> True)) = { valid_rewrite_valid = (fun h b pos pos' -> v.valid_rewrite_valid h b pos pos'); valid_rewrite_size = (fun x -> v.valid_rewrite_size x); } let valid_rewrite_compose (p1: parser) (p2: parser) (precond12: pre_t p1) (f12: (x: Parser?.t p1 { precond12 x }) -> GTot (Parser?.t p2)) (v12: valid_rewrite_t p1 p2 precond12 f12) (p3: parser) (precond23: pre_t p2) (f23: (x: Parser?.t p2 { precond23 x }) -> GTot (Parser?.t p3)) (v23: valid_rewrite_t p2 p3 precond23 f23) : Tot (valid_rewrite_t p1 p3 (fun x -> precond12 x /\ precond23 (f12 x)) (fun x -> f23 (f12 x))) = { valid_rewrite_valid = (fun h b pos pos' -> v12.valid_rewrite_valid h b pos pos'; v23.valid_rewrite_valid h b pos pos' ); valid_rewrite_size = (fun x -> v12.valid_rewrite_size x; v23.valid_rewrite_size (f12 x) ); } let valid_rewrite_spec (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) : Tot (repr_spec unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False)) = fun vin -> v.valid_rewrite_size vin; Correct ((), f vin) inline_for_extraction val valid_rewrite_impl (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) : Tot (repr_impl unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False) inv (valid_rewrite_spec p1 p2 precond f v)) inline_for_extraction let valid_rewrite_repr (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) : Tot (repr unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) (fun _ -> False) inv) = Repr _ (valid_rewrite_impl p1 p2 precond f v inv) inline_for_extraction let valid_rewrite (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (inv: memory_invariant) (v: valid_rewrite_t p1 p2 precond f) : Write unit p1 (p2) precond (fun vin _ vout -> precond vin /\ f vin == vout) inv = EWrite?.reflect (valid_rewrite_repr p1 p2 precond f v inv) inline_for_extraction val cast (p1: parser) (p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) (v: valid_rewrite_t p1 p2 precond f) (inv: memory_invariant) (x1: ptr p1 inv { precond (deref_spec x1) }) : Tot (x2: ptr p2 inv { deref_spec x2 == f (deref_spec x1) }) let valid_rewrite_parse_pair_assoc_1 (p1 p2 p3: parser) : Tot (valid_rewrite_t ((p1 `parse_pair` p2) `parse_pair` p3) (p1 `parse_pair` (p2 `parse_pair` p3)) (fun _ -> True) (fun ((x1, x2), x3) -> (x1, (x2, x3)))) = { valid_rewrite_valid = (fun h b pos pos' -> let pos3 = valid_parse_pair_inv_spec h (p1 `parse_pair` p2) p3 b pos pos' in let pos2 = valid_parse_pair_inv_spec h p1 p2 b pos pos3 in valid_parse_pair p2 p3 h b pos2 pos3 pos'; valid_parse_pair p1 (p2 `parse_pair` p3) h b pos pos2 pos' ); valid_rewrite_size = (fun _ -> ()); } let valid_rewrite_parse_pair_assoc_2 (p1 p2 p3: parser) : Tot (valid_rewrite_t (p1 `parse_pair` (p2 `parse_pair` p3)) ((p1 `parse_pair` p2) `parse_pair` p3) (fun _ -> True) (fun (x1, (x2, x3)) -> ((x1, x2), x3))) = { valid_rewrite_valid = (fun h b pos pos' -> let pos2 = valid_parse_pair_inv_spec h p1 (p2 `parse_pair` p3) b pos pos' in let pos3 = valid_parse_pair_inv_spec h p2 p3 b pos2 pos' in valid_parse_pair p1 p2 h b pos pos2 pos3; valid_parse_pair (p1 `parse_pair` p2) p3 h b pos pos3 pos' ); valid_rewrite_size = (fun _ -> ()); } let valid_rewrite_parse_pair_compat_l (p: parser) (p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) ($v: valid_rewrite_t p1 p2 precond f) : Tot (valid_rewrite_t (p `parse_pair` p1) (p `parse_pair` p2) (fun (_, x) -> precond x) (fun (x, y) -> (x, f y))) = { valid_rewrite_valid = (fun h b pos pos' -> let posq = valid_parse_pair_inv_spec h p p1 b pos pos' in v.valid_rewrite_valid h b posq pos'; valid_parse_pair p p2 h b pos posq pos' ); valid_rewrite_size = (fun xy -> let (_, x) = (xy <: Parser?.t (p `parse_pair` p1)) in v.valid_rewrite_size x ); } let valid_rewrite_parse_pair_compat_r (p: parser) (p1 p2: parser) (precond: pre_t p1) (f: (x: Parser?.t p1 { precond x }) -> GTot (Parser?.t p2)) ($v: valid_rewrite_t p1 p2 precond f) : Tot (valid_rewrite_t (p1 `parse_pair` p) (p2 `parse_pair` p) (fun (x, _) -> precond x) (fun (x, y) -> (f x, y))) = { valid_rewrite_valid = (fun h b pos pos' -> let posp = valid_parse_pair_inv_spec h p1 p b pos pos' in v.valid_rewrite_valid h b pos posp; valid_parse_pair p2 p h b pos posp pos' ); valid_rewrite_size = (fun xy -> let (x, _) = (xy <: Parser?.t (p1 `parse_pair` p)) in v.valid_rewrite_size x ); } let check_precond_t (p1: parser) (precond: pre_t p1) : Tot Type = (b: B.buffer U8.t) -> (len: U32.t { B.len b == len }) -> (pos: U32.t) -> (pos' : U32.t) -> HST.Stack bool (requires (fun h -> valid_pos p1 h b pos pos' )) (ensures (fun h res h' -> B.modifies B.loc_none h h' /\ (res == true <==> precond (contents p1 h b pos pos')) )) inline_for_extraction val check_precond_repr (p1: parser) (precond: pre_t p1) (c: check_precond_t p1 precond) (inv: memory_invariant) : Tot (repr unit p1 (p1) precond (fun vin _ vout -> vin == vout /\ precond vin) (fun vin -> ~ (precond vin)) inv) inline_for_extraction let check_precond (p1: parser) (precond: pre_t p1) (c: check_precond_t p1 precond) (inv: memory_invariant) : EWrite unit p1 (p1) precond (fun vin _ vout -> vin == vout /\ precond vin) (fun vin -> ~ (precond vin)) inv = EWrite?.reflect (check_precond_repr p1 precond c inv)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "LowStar.Buffer.fst.checked", "LowParseWriters.LowParse.fsti.checked", "FStar.UInt8.fsti.checked", "FStar.UInt32.fsti.checked", "FStar.Tactics.V2.fst.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": false, "source_file": "LowParseWriters.fsti" }
[ { "abbrev": false, "full_module": "FStar.Monotonic.Pure", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "short_module": null }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.UInt32", "short_module": "U32" }, { "abbrev": true, "full_module": "FStar.UInt8", "short_module": "U8" }, { "abbrev": true, "full_module": "LowStar.Buffer", "short_module": "B" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": false, "full_module": "FStar.HyperStack.ST", "short_module": null }, { "abbrev": false, "full_module": "LowParseWriters.LowParse", "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: LowParseWriters.ptr p inv -> LowParseWriters.repr_spec Prims.unit LowParseWriters.LowParse.parse_empty p (fun _ -> Prims.l_True) (fun _ _ vout -> vout == LowParseWriters.deref_spec x) (fun _ -> Prims.l_False)
Prims.Tot
[ "total" ]
[]
[ "LowParseWriters.memory_invariant", "LowParseWriters.LowParse.parser", "LowParseWriters.ptr", "LowParseWriters.LowParse.__proj__Parser__item__t", "LowParseWriters.LowParse.parse_empty", "LowParseWriters.Correct", "FStar.Pervasives.Native.tuple2", "Prims.unit", "FStar.Pervasives.Native.Mktuple2", "LowParseWriters.deref_spec", "LowParseWriters.result", "LowParseWriters.repr_spec", "Prims.l_True", "Prims.eq2", "Prims.l_False" ]
[]
false
false
false
false
false
let cat_spec (#inv: memory_invariant) (#p: parser) (x: ptr p inv) : Tot (repr_spec unit parse_empty p (fun _ -> True) (fun _ _ vout -> vout == deref_spec x) (fun _ -> False)) =
fun _ -> Correct ((), deref_spec x)
false
Steel.Effect.Atomic.fst
Steel.Effect.Atomic.intro_vrewrite
val intro_vrewrite (#opened:inames) (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : SteelGhost unit opened v (fun _ -> vrewrite v f) (fun _ -> True) (fun h _ h' -> h' (vrewrite v f) == f (h v))
val intro_vrewrite (#opened:inames) (v: vprop) (#t: Type) (f: (normal (t_of v) -> GTot t)) : SteelGhost unit opened v (fun _ -> vrewrite v f) (fun _ -> True) (fun h _ h' -> h' (vrewrite v f) == f (h v))
let intro_vrewrite v #t f = let x : Ghost.erased (t_of v) = gget v in let x' : Ghost.erased t = Ghost.hide (f (Ghost.reveal x)) in change_slprop v (vrewrite v f) x x' (fun m -> vrewrite_sel_eq v f m )
{ "file_name": "lib/steel/Steel.Effect.Atomic.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 5, "end_line": 853, "start_col": 0, "start_line": 842 }
(* 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.Atomic open Steel.Effect friend Steel.Effect #set-options "--warn_error -330" //turn off the experimental feature warning let _ : squash (forall (pre:pre_t) (m0:mem{interp (hp_of pre) m0}) (m1:mem{disjoint m0 m1}). mk_rmem pre m0 == mk_rmem pre (join m0 m1)) = Classical.forall_intro rmem_depends_only_on let req_to_act_req (#pre:vprop) (req:req_t pre) : mprop (hp_of pre) = fun m -> rmem_depends_only_on pre; interp (hp_of pre) m /\ req (mk_rmem pre m) unfold let to_post (#a:Type) (post:post_t a) = fun x -> (hp_of (post x)) let ens_to_act_ens (#pre:pre_t) (#a:Type) (#post:post_t a) (ens:ens_t pre a post) : mprop2 (hp_of pre) (to_post post) = fun m0 x m1 -> interp (hp_of pre) m0 /\ interp (hp_of (post x)) m1 /\ ens (mk_rmem pre m0) x (mk_rmem (post x) m1) let repr a framed opened f pre post req ens = action_except_full a opened (hp_of pre) (to_post post) (req_to_act_req req) (ens_to_act_ens ens) let return_ a x opened #p = fun _ -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (p x) (core_mem m0) in lemma_frame_equalities_refl (p x) h0; x #push-options "--fuel 0 --ifuel 0" #push-options "--z3rlimit 20 --fuel 1 --ifuel 1" val frame00 (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:repr a framed opened obs pre post req ens) (frame:vprop) : repr a true opened obs (pre `star` frame) (fun x -> post x `star` frame) (fun h -> req (focus_rmem h pre)) (fun h0 r h1 -> req (focus_rmem h0 pre) /\ ens (focus_rmem h0 pre) r (focus_rmem h1 (post r)) /\ frame_opaque frame (focus_rmem h0 frame) (focus_rmem h1 frame)) module Sem = Steel.Semantics.Hoare.MST module Mem = Steel.Memory let equiv_middle_left_assoc (a b c d:slprop) : Lemma (((a `Mem.star` b) `Mem.star` c `Mem.star` d) `Mem.equiv` (a `Mem.star` (b `Mem.star` c) `Mem.star` d)) = let open Steel.Memory in star_associative a b c; star_congruence ((a `star` b) `star` c) d (a `star` (b `star` c)) d let frame00 #a #framed #opened #obs #pre #post #req #ens f frame = fun frame' -> let m0:full_mem = NMSTTotal.get () in let snap:rmem frame = mk_rmem frame (core_mem m0) in // Need to define it with type annotation, although unused, for it to trigger // the pattern on the framed ensures in the def of MstTot let aux:mprop (hp_of frame `Mem.star` frame') = req_frame frame snap in focus_is_restrict_mk_rmem (pre `star` frame) pre (core_mem m0); assert (interp (hp_of (pre `star` frame) `Mem.star` frame' `Mem.star` locks_invariant opened m0) m0); equiv_middle_left_assoc (hp_of pre) (hp_of frame) frame' (locks_invariant opened m0); assert (interp (hp_of pre `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m0) m0); let x = f (hp_of frame `Mem.star` frame') in let m1:full_mem = NMSTTotal.get () in assert (interp (hp_of (post x) `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m1) m1); equiv_middle_left_assoc (hp_of (post x)) (hp_of frame) frame' (locks_invariant opened m1); assert (interp ((hp_of (post x) `Mem.star` hp_of frame) `Mem.star` frame' `Mem.star` locks_invariant opened m1) m1); focus_is_restrict_mk_rmem (pre `star` frame) frame (core_mem m0); focus_is_restrict_mk_rmem (post x `star` frame) frame (core_mem m1); let h0:rmem (pre `star` frame) = mk_rmem (pre `star` frame) (core_mem m0) in let h1:rmem (post x `star` frame) = mk_rmem (post x `star` frame) (core_mem m1) in assert (focus_rmem h0 frame == focus_rmem h1 frame); focus_is_restrict_mk_rmem (post x `star` frame) (post x) (core_mem m1); lemma_frame_opaque_refl frame (focus_rmem h0 frame); x unfold let bind_req_opaque (#a:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#pr:a -> prop) (req_g:(x:a -> req_t (pre_g x))) (frame_f:vprop) (frame_g:a -> vprop) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) : req_t (pre_f `star` frame_f) = fun m0 -> req_f (focus_rmem m0 pre_f) /\ (forall (x:a) (h1:hmem (post_f x `star` frame_f)). (ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f)) ==> pr x /\ (can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); (req_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)))) unfold let bind_ens_opaque (#a:Type) (#b:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#pr:a -> prop) (ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (frame_f:vprop) (frame_g:a -> vprop) (post:post_t b) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (_:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) : ens_t (pre_f `star` frame_f) b post = fun m0 y m2 -> req_f (focus_rmem m0 pre_f) /\ (exists (x:a) (h1:hmem (post_f x `star` frame_f)). pr x /\ ( can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f) /\ frame_opaque (frame_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (frame_g x)) (focus_rmem m2 (frame_g x)) /\ ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ (ens_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)) y (focus_rmem m2 (post_g x y)))) val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True) #push-options "--z3rlimit 20" let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_f x `star` frame_f) (core_mem m1) in let h1' = mk_rmem (pre_g x `star` frame_g x) (core_mem m1) in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); focus_is_restrict_mk_rmem (post_f x `star` frame_f) (pre_g x `star` frame_g x) (core_mem m1); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of (post_f x `star` frame_f)) (hp_of (pre_g x `star` frame_g x)) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); let h2' = mk_rmem (post_g x y `star` frame_g x) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in // assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem (post_g x y `star` frame_g x) (post y) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of (post_g x y `star` frame_g x)) (hp_of (post y)) frame (locks_invariant opened m2) m2; y let norm_repr (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) (f:repr a framed opened obs pre post req ens) : repr a framed opened obs pre post (fun h -> norm_opaque (req h)) (fun h0 x h1 -> norm_opaque (ens h0 x h1)) = f let bind a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = norm_repr (bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g) val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True) let subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem pre_g (core_mem m0) in can_be_split_trans pre_g (pre_f `star` fr) pre_f; can_be_split_trans pre_g (pre_f `star` fr) fr; can_be_split_3_interp (hp_of pre_g) (hp_of (pre_f `star` fr)) frame (locks_invariant opened m0) m0; focus_replace pre_g (pre_f `star` fr) pre_f (core_mem m0); let x = frame00 f fr frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_g x) (core_mem m1) in let h0' = mk_rmem (pre_f `star` fr) (core_mem m0) in let h1' = mk_rmem (post_f x `star` fr) (core_mem m1) in // From frame00 assert (frame_opaque fr (focus_rmem h0' fr) (focus_rmem h1' fr)); // Replace h0'/h1' by h0/h1 focus_replace pre_g (pre_f `star` fr) fr (core_mem m0); focus_replace (post_g x) (post_f x `star` fr) fr (core_mem m1); assert (frame_opaque fr (focus_rmem h0 fr) (focus_rmem h1 fr)); can_be_split_trans (post_g x) (post_f x `star` fr) (post_f x); can_be_split_trans (post_g x) (post_f x `star` fr) fr; can_be_split_3_interp (hp_of (post_f x `star` fr)) (hp_of (post_g x)) frame (locks_invariant opened m1) m1; focus_replace (post_g x) (post_f x `star` fr) (post_f x) (core_mem m1); x let subcomp a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #_ #pr #p1 #p2 f = lemma_subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2; subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f #pop-options let bind_pure_steela_ a b opened o #wp f g = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun frame -> let x = f () in g x frame let lift_ghost_atomic a o f = f let lift_atomic_steel a o f = f let as_atomic_action f = SteelAtomic?.reflect f let as_atomic_action_ghost f = SteelGhost?.reflect f let as_atomic_unobservable_action f = SteelAtomicU?.reflect f (* Some helpers *) let get0 (#opened:inames) (#p:vprop) (_:unit) : repr (erased (rmem p)) true opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1 /\ frame_equalities p r h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; h0 let get () = SteelGhost?.reflect (get0 ()) let intro_star (p q:vprop) (r:slprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (m:mem) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m) ) : Lemma (requires interp ((hp_of p) `Mem.star` r) m /\ sel_of p m == reveal vp) (ensures interp ((hp_of q) `Mem.star` r) m) = let p = hp_of p in let q = hp_of q in let intro (ml mr:mem) : Lemma (requires interp q ml /\ interp r mr /\ disjoint ml mr) (ensures disjoint ml mr /\ interp (q `Mem.star` r) (Mem.join ml mr)) = Mem.intro_star q r ml mr in elim_star p r m; Classical.forall_intro (Classical.move_requires proof); Classical.forall_intro_2 (Classical.move_requires_2 intro) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let rewrite_slprop0 (#opened:inames) (p q:vprop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ _ -> True) = fun frame -> let m:full_mem = NMSTTotal.get () in proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) (sel_of q m) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let rewrite_slprop p q l = SteelGhost?.reflect (rewrite_slprop0 p q l) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop0 (#opened:inames) (p q:vprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun h -> h p == reveal vp) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) vp vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop p q vp vq l = SteelGhost?.reflect (change_slprop0 p q vp vq l) let change_equal_slprop p q = let m = get () in let x : Ghost.erased (t_of p) = hide ((reveal m) p) in let y : Ghost.erased (t_of q) = Ghost.hide (Ghost.reveal x) in change_slprop p q x y (fun _ -> ()) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop_20 (#opened:inames) (p q:vprop) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop_2 p q vq l = SteelGhost?.reflect (change_slprop_20 p q vq l) let change_slprop_rel0 (#opened:inames) (p q:vprop) (rel : normal (t_of p) -> normal (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel p q rel proof = SteelGhost?.reflect (change_slprop_rel0 p q rel proof) let change_slprop_rel_with_cond0 (#opened:inames) (p q:vprop) (cond: t_of p -> prop) (rel : (t_of p) -> (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ cond (sel_of p m)) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun m -> cond (m p)) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel_with_cond p q cond rel proof = SteelGhost?.reflect (change_slprop_rel_with_cond0 p q cond rel proof) let extract_info0 (#opened:inames) (p:vprop) (vp:erased (normal (t_of p))) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> h p == reveal vp) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info p vp fact l = SteelGhost?.reflect (extract_info0 p vp fact l) let extract_info_raw0 (#opened:inames) (p:vprop) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> True) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info_raw p fact l = SteelGhost?.reflect (extract_info_raw0 p fact l) let noop _ = change_slprop_rel emp emp (fun _ _ -> True) (fun _ -> ()) let sladmit _ = SteelGhostF?.reflect (fun _ -> NMSTTotal.nmst_tot_admit ()) let slassert0 (#opened:inames) (p:vprop) : repr unit false opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0 let slassert p = SteelGhost?.reflect (slassert0 p) let drop p = rewrite_slprop p emp (fun m -> emp_unit (hp_of p); affine_star (hp_of p) Mem.emp m; reveal_emp()) let reveal_star0 (#opened:inames) (p1 p2:vprop) : repr unit false opened Unobservable (p1 `star` p2) (fun _ -> p1 `star` p2) (fun _ -> True) (fun h0 _ h1 -> h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 (p1 `star` p2) == (h0 p1, h0 p2) /\ h1 (p1 `star` p2) == (h1 p1, h1 p2) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem let reveal_star p1 p2 = SteelGhost?.reflect (reveal_star0 p1 p2) let reveal_star_30 (#opened:inames) (p1 p2 p3:vprop) : repr unit false opened Unobservable (p1 `star` p2 `star` p3) (fun _ -> p1 `star` p2 `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split (p1 `star` p2 `star` p3) p1 /\ can_be_split (p1 `star` p2 `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 (p1 `star` p2 `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 (p1 `star` p2 `star` p3) == ((h1 p1, h1 p2), h1 p3) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem (p1 `star` p2 `star` p3) (core_mem m) in can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p1; can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p2; reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2 `star` p3); reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2); reveal_mk_rmem (p1 `star` p2 `star` p3) m p3 let reveal_star_3 p1 p2 p3 = SteelGhost?.reflect (reveal_star_30 p1 p2 p3) let intro_pure p = rewrite_slprop emp (pure p) (fun m -> pure_interp p m) let elim_pure_aux #uses (p:prop) : SteelGhostT (_:unit{p}) uses (pure p) (fun _ -> to_vprop Mem.emp) = as_atomic_action_ghost (Steel.Memory.elim_pure #uses p) let elim_pure #uses p = let _ = elim_pure_aux p in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()) let return #a #opened #p x = SteelAtomicBase?.reflect (return_ a x opened #p) let intro_exists #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists x (h_exists_sl' p) m) let intro_exists_erased #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists (Ghost.reveal x) (h_exists_sl' p) m) let witness_exists #a #u #p _ = SteelGhost?.reflect (Steel.Memory.witness_h_exists #u (fun x -> hp_of (p x))) let lift_exists #a #u p = as_atomic_action_ghost (Steel.Memory.lift_h_exists #u (fun x -> hp_of (p x))) let exists_equiv p q = Classical.forall_intro_2 reveal_equiv; h_exists_cong (h_exists_sl' p) (h_exists_sl' q) let exists_cong p q = rewrite_slprop (h_exists p) (h_exists q) (fun m -> reveal_equiv (h_exists p) (h_exists q); exists_equiv p q) let fresh_invariant #uses p ctxt = rewrite_slprop p (to_vprop (hp_of p)) (fun _ -> ()); let i = as_atomic_unobservable_action (fresh_invariant uses (hp_of p) ctxt) in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()); return i let new_invariant #uses p = let i = fresh_invariant #uses p [] in return i (* * AR: SteelAtomic and SteelGhost are not marked reifiable since we intend to run Steel programs natively * However to implement the with_inv combinators we need to reify their thunks to reprs * We could implement it better by having support for reification only in the .fst file * But for now assuming a function *) assume val reify_steel_atomic_comp (#a:Type) (#framed:bool) (#opened_invariants:inames) (#g:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:unit -> SteelAtomicBase a framed opened_invariants g pre post req ens) : repr a framed opened_invariants g pre post req ens [@@warn_on_use "as_unobservable_atomic_action is a trusted primitive"] let as_atomic_o_action (#a:Type u#a) (#opened_invariants:inames) (#fp:slprop) (#fp': a -> slprop) (o:observability) (f:action_except a opened_invariants fp fp') : SteelAtomicBaseT a opened_invariants o (to_vprop fp) (fun x -> to_vprop (fp' x)) = SteelAtomicBaseT?.reflect f let with_invariant #a #fp #fp' #obs #opened #p i f = rewrite_slprop fp (to_vprop (hp_of fp)) (fun _ -> ()); let x = as_atomic_o_action obs (Steel.Memory.with_invariant #a #(hp_of fp) #(fun x -> hp_of (fp' x)) #opened #(hp_of p) i (reify_steel_atomic_comp f)) in rewrite_slprop (to_vprop (hp_of (fp' x))) (fp' x) (fun _ -> ()); return x assume val reify_steel_ghost_comp (#a:Type) (#framed:bool) (#opened_invariants:inames) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:unit -> SteelGhostBase a framed opened_invariants Unobservable pre post req ens) : repr a framed opened_invariants Unobservable pre post req ens let with_invariant_g #a #fp #fp' #opened #p i f = rewrite_slprop fp (to_vprop (hp_of fp)) (fun _ -> ()); let x = as_atomic_unobservable_action (Steel.Memory.with_invariant #a #(hp_of fp) #(fun x -> hp_of (fp' x)) #opened #(hp_of p) i (reify_steel_ghost_comp f)) in rewrite_slprop (to_vprop (hp_of (fp' x))) (fp' x) (fun _ -> ()); return (hide x) let intro_vrefine v p = let m = get () in let x : Ghost.erased (t_of v) = gget v in let x' : Ghost.erased (vrefine_t v p) = Ghost.hide (Ghost.reveal x) in change_slprop v (vrefine v p) x x' (fun m -> interp_vrefine_hp v p m; vrefine_sel_eq v p m ) let elim_vrefine v p = let h = get() in let x : Ghost.erased (vrefine_t v p) = gget (vrefine v p) in let x' : Ghost.erased (t_of v) = Ghost.hide (Ghost.reveal x) in change_slprop (vrefine v p) v x x' (fun m -> interp_vrefine_hp v p m; vrefine_sel_eq v p m ) let vdep_cond (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (x1: t_of (v `star` q)) : Tot prop = q == p (fst x1) let vdep_rel (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (x1: t_of (v `star` q)) (x2: (t_of (vdep v p))) : Tot prop = q == p (fst x1) /\ dfst (x2 <: (dtuple2 (t_of v) (vdep_payload v p))) == fst x1 /\ dsnd (x2 <: (dtuple2 (t_of v) (vdep_payload v p))) == snd x1 let intro_vdep_lemma (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (m: mem) : Lemma (requires ( interp (hp_of (v `star` q)) m /\ q == p (fst (sel_of (v `star` q) m)) )) (ensures ( interp (hp_of (v `star` q)) m /\ interp (hp_of (vdep v p)) m /\ vdep_rel v q p (sel_of (v `star` q) m) (sel_of (vdep v p) m) )) = Mem.interp_star (hp_of v) (hp_of q) m; interp_vdep_hp v p m; vdep_sel_eq v p m let intro_vdep v q p = reveal_star v q; change_slprop_rel_with_cond (v `star` q) (vdep v p) (vdep_cond v q p) (vdep_rel v q p) (fun m -> intro_vdep_lemma v q p m) let vdep_cond_recip (v: vprop) (p: (t_of v -> Tot vprop)) (q: vprop) (x2: t_of (vdep v p)) : Tot prop = q == p (dfst (x2 <: dtuple2 (t_of v) (vdep_payload v p))) let vdep_rel_recip (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (x2: (t_of (vdep v p))) (x1: t_of (v `star` q)) : Tot prop = vdep_rel v q p x1 x2 let elim_vdep_lemma (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (m: mem) : Lemma (requires ( interp (hp_of (vdep v p)) m /\ q == p (dfst (sel_of (vdep v p) m <: dtuple2 (t_of v) (vdep_payload v p))) )) (ensures ( interp (hp_of (v `star` q)) m /\ interp (hp_of (vdep v p)) m /\ vdep_rel v q p (sel_of (v `star` q) m) (sel_of (vdep v p) m) )) = Mem.interp_star (hp_of v) (hp_of q) m; interp_vdep_hp v p m; vdep_sel_eq v p m let elim_vdep0 (#opened:inames) (v: vprop) (p: (t_of v -> Tot vprop)) (q: vprop) : SteelGhost unit opened (vdep v p) (fun _ -> v `star` q) (requires (fun h -> q == p (dfst (h (vdep v p))))) (ensures (fun h _ h' -> let fs = h' v in let sn = h' q in let x2 = h (vdep v p) in q == p fs /\ dfst x2 == fs /\ dsnd x2 == sn )) = change_slprop_rel_with_cond (vdep v p) (v `star` q) (vdep_cond_recip v p q) (vdep_rel_recip v q p) (fun m -> elim_vdep_lemma v q p m); reveal_star v q let elim_vdep v p = let r = gget (vdep v p) in let res = Ghost.hide (dfst #(t_of v) #(vdep_payload v p) (Ghost.reveal r)) in elim_vdep0 v p (p (Ghost.reveal res)); res
{ "checked_file": "/", "dependencies": [ "Steel.Semantics.Hoare.MST.fst.checked", "Steel.Memory.fsti.checked", "Steel.Effect.fst.checked", "Steel.Effect.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.NMSTTotal.fst.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "Steel.Effect.Atomic.fst" }
[ { "abbrev": true, "full_module": "FStar.Universe", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "Steel.Semantics.Hoare.MST", "short_module": "Sem" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Common", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics", "short_module": "T" }, { "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": 1, "initial_ifuel": 1, "max_fuel": 1, "max_ifuel": 1, "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": 20, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
v: Steel.Effect.Common.vprop -> f: (_: Steel.Effect.Common.normal (Steel.Effect.Common.t_of v) -> Prims.GTot t) -> Steel.Effect.Atomic.SteelGhost Prims.unit
Steel.Effect.Atomic.SteelGhost
[]
[]
[ "Steel.Memory.inames", "Steel.Effect.Common.vprop", "Steel.Effect.Common.normal", "Steel.Effect.Common.t_of", "Steel.Effect.Atomic.change_slprop", "Steel.Effect.Common.vrewrite", "Steel.Memory.mem", "Steel.Effect.Common.vrewrite_sel_eq", "Prims.unit", "FStar.Ghost.erased", "FStar.Ghost.hide", "FStar.Ghost.reveal", "Steel.Effect.Atomic.gget" ]
[]
false
true
false
false
false
let intro_vrewrite v #t f =
let x:Ghost.erased (t_of v) = gget v in let x':Ghost.erased t = Ghost.hide (f (Ghost.reveal x)) in change_slprop v (vrewrite v f) x x' (fun m -> vrewrite_sel_eq v f m)
false
CanonDeep.fst
CanonDeep.canon_point
val canon_point (e: expr) : Dv expr
val canon_point (e: expr) : Dv expr
let rec canon_point (e:expr) : Dv expr = match e with // Evaluate constants | Plus (Lit a) (Lit b) -> Lit (a + b) | Mult (Lit a) (Lit b) -> Lit (a * b) // Forget about negations | Neg e -> canon_point (Mult (Lit (-1)) e) // Distribute | Mult a (Plus b c) -> let l = canon_point (Mult a b) in let r = canon_point (Mult a c) in canon_point (Plus l r) | Mult (Plus a b) c -> let l = canon_point (Mult a c) in let r = canon_point (Mult b c) in canon_point (Plus l r) // Associate to the left | Mult a (Mult b c) -> let l = canon_point (Mult a b) in let r = canon_point c in canon_point (Mult l r) | Plus a (Plus b c) -> let l = canon_point (Plus a b) in let r = canon_point c in canon_point (Plus l r) | Plus (Plus a b) c -> if O.gt (compare_expr b c) then begin let l = canon_point (Plus a c) in Plus l b end else e | Mult (Mult a b) c -> if O.gt (compare_expr b c) then begin let l = canon_point (Mult a c) in Mult l b end else e | Plus a (Lit 0) -> a | Plus (Lit 0) b -> b | Plus a b -> if O.gt (compare_expr a b) then Plus b a else e | Mult (Lit 0) _ -> Lit 0 | Mult _ (Lit 0) -> Lit 0 | Mult (Lit 1) r -> r | Mult l (Lit 1) -> l | Mult a b -> if O.gt (compare_expr a b) then Mult b a else e // Forget about subtraction | Minus a b -> let r = canon_point (Neg b) in canon_point (Plus a r) | _ -> e
{ "file_name": "examples/native_tactics/CanonDeep.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 10, "end_line": 115, "start_col": 0, "start_line": 44 }
(* Copyright 2008-2018 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 CanonDeep open FStar.Tactics.V2 open FStar.Reflection.V2 open FStar.Reflection.V2.Arith open FStar.Mul module O = FStar.Order (* TODO: The implementation from FStar.Tactics.Canon seems *very* naive: - it repeatedly turns terms into exprs, but that's fixed below - more importantly, a lot of work seems to be done repeatedly and the computational complexity seems huge, way over O(n^2) + terms are traversed both bottom-up and top-down, so that's already O(n^2) + canon_point calls itself recursively even on exprs of the same size Q: Is there a good canonicalization algorithm from literature we can use? A purely functional one? *) (* TODO: expr is mixing up int and uint expressions in a very untyped way. How am I supposed to write an expr_to_term function that always produces well-typed terms when types are so badly mixed up? Tempted to roll out a simplified version of expr that only does ints. *) (* This is the pure part of canon_point *) (* TODO: need Tot to state the lemma Nik wanted, but order not easy at all, and I'm not sure it's worth investing time in verifying such a bad algorithm *)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Reflection.V2.Arith.fst.checked", "FStar.Reflection.V2.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Order.fst.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "CanonDeep.fst" }
[ { "abbrev": true, "full_module": "FStar.Order", "short_module": "O" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Reflection.V2.Arith", "short_module": null }, { "abbrev": false, "full_module": "FStar.Reflection.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "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: FStar.Reflection.V2.Arith.expr -> FStar.Pervasives.Dv FStar.Reflection.V2.Arith.expr
FStar.Pervasives.Dv
[]
[]
[ "FStar.Reflection.V2.Arith.expr", "Prims.int", "FStar.Reflection.V2.Arith.Lit", "Prims.op_Addition", "FStar.Mul.op_Star", "CanonDeep.canon_point", "FStar.Reflection.V2.Arith.Mult", "Prims.op_Minus", "FStar.Reflection.V2.Arith.Plus", "FStar.Order.gt", "FStar.Reflection.V2.Arith.compare_expr", "Prims.bool", "FStar.Reflection.V2.Arith.Neg" ]
[ "recursion" ]
false
true
false
false
false
let rec canon_point (e: expr) : Dv expr =
match e with | Plus (Lit a) (Lit b) -> Lit (a + b) | Mult (Lit a) (Lit b) -> Lit (a * b) | Neg e -> canon_point (Mult (Lit (- 1)) e) | Mult a (Plus b c) -> let l = canon_point (Mult a b) in let r = canon_point (Mult a c) in canon_point (Plus l r) | Mult (Plus a b) c -> let l = canon_point (Mult a c) in let r = canon_point (Mult b c) in canon_point (Plus l r) | Mult a (Mult b c) -> let l = canon_point (Mult a b) in let r = canon_point c in canon_point (Mult l r) | Plus a (Plus b c) -> let l = canon_point (Plus a b) in let r = canon_point c in canon_point (Plus l r) | Plus (Plus a b) c -> if O.gt (compare_expr b c) then let l = canon_point (Plus a c) in Plus l b else e | Mult (Mult a b) c -> if O.gt (compare_expr b c) then let l = canon_point (Mult a c) in Mult l b else e | Plus a (Lit 0) -> a | Plus (Lit 0) b -> b | Plus a b -> if O.gt (compare_expr a b) then Plus b a else e | Mult (Lit 0) _ -> Lit 0 | Mult _ (Lit 0) -> Lit 0 | Mult (Lit 1) r -> r | Mult l (Lit 1) -> l | Mult a b -> if O.gt (compare_expr a b) then Mult b a else e | Minus a b -> let r = canon_point (Neg b) in canon_point (Plus a r) | _ -> e
false
CanonDeep.fst
CanonDeep.check_canon_deep
val check_canon_deep : _: Prims.unit -> FStar.Tactics.Effect.Tac Prims.unit
let check_canon_deep () = canon_deep (); or_else qed (fun () -> dump "`canon deep` left the following goals"; fail "")
{ "file_name": "examples/native_tactics/CanonDeep.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 31, "end_line": 203, "start_col": 0, "start_line": 199 }
(* Copyright 2008-2018 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 CanonDeep open FStar.Tactics.V2 open FStar.Reflection.V2 open FStar.Reflection.V2.Arith open FStar.Mul module O = FStar.Order (* TODO: The implementation from FStar.Tactics.Canon seems *very* naive: - it repeatedly turns terms into exprs, but that's fixed below - more importantly, a lot of work seems to be done repeatedly and the computational complexity seems huge, way over O(n^2) + terms are traversed both bottom-up and top-down, so that's already O(n^2) + canon_point calls itself recursively even on exprs of the same size Q: Is there a good canonicalization algorithm from literature we can use? A purely functional one? *) (* TODO: expr is mixing up int and uint expressions in a very untyped way. How am I supposed to write an expr_to_term function that always produces well-typed terms when types are so badly mixed up? Tempted to roll out a simplified version of expr that only does ints. *) (* This is the pure part of canon_point *) (* TODO: need Tot to state the lemma Nik wanted, but order not easy at all, and I'm not sure it's worth investing time in verifying such a bad algorithm *) let rec canon_point (e:expr) : Dv expr = match e with // Evaluate constants | Plus (Lit a) (Lit b) -> Lit (a + b) | Mult (Lit a) (Lit b) -> Lit (a * b) // Forget about negations | Neg e -> canon_point (Mult (Lit (-1)) e) // Distribute | Mult a (Plus b c) -> let l = canon_point (Mult a b) in let r = canon_point (Mult a c) in canon_point (Plus l r) | Mult (Plus a b) c -> let l = canon_point (Mult a c) in let r = canon_point (Mult b c) in canon_point (Plus l r) // Associate to the left | Mult a (Mult b c) -> let l = canon_point (Mult a b) in let r = canon_point c in canon_point (Mult l r) | Plus a (Plus b c) -> let l = canon_point (Plus a b) in let r = canon_point c in canon_point (Plus l r) | Plus (Plus a b) c -> if O.gt (compare_expr b c) then begin let l = canon_point (Plus a c) in Plus l b end else e | Mult (Mult a b) c -> if O.gt (compare_expr b c) then begin let l = canon_point (Mult a c) in Mult l b end else e | Plus a (Lit 0) -> a | Plus (Lit 0) b -> b | Plus a b -> if O.gt (compare_expr a b) then Plus b a else e | Mult (Lit 0) _ -> Lit 0 | Mult _ (Lit 0) -> Lit 0 | Mult (Lit 1) r -> r | Mult l (Lit 1) -> l | Mult a b -> if O.gt (compare_expr a b) then Mult b a else e // Forget about subtraction | Minus a b -> let r = canon_point (Neg b) in canon_point (Plus a r) | _ -> e (* This is trying to emulate `pointwise canon_point` *) let rec canon_expr (e:expr) : Dv expr = match e with | Atom _ _ | Lit _ -> e | Plus l r -> canon_point (Plus (canon_expr l) (canon_expr r)) | Minus l r -> canon_point (Minus (canon_expr l) (canon_expr r)) | Mult l r -> canon_point (Mult (canon_expr l) (canon_expr r)) | Neg l -> canon_point (Neg (canon_expr l)) | Land l r -> canon_point (Land (canon_expr l) (canon_expr r)) | Lor l r -> canon_point (Lor (canon_expr l) (canon_expr r)) | Lxor l r -> canon_point (Lxor (canon_expr l) (canon_expr r)) | Ladd l r -> canon_point (Ladd (canon_expr l) (canon_expr r)) | Lsub l r -> canon_point (Lsub (canon_expr l) (canon_expr r)) | Shl l r -> canon_point (Shl (canon_expr l) (canon_expr r)) | Shr l r -> canon_point (Shr (canon_expr l) (canon_expr r)) | NatToBv l -> canon_point (NatToBv (canon_expr l)) | Udiv l r -> canon_point (Udiv (canon_expr l) (canon_expr r)) | Umod l r -> canon_point (Umod (canon_expr l) (canon_expr r)) | MulMod l r -> canon_point (MulMod (canon_expr l) (canon_expr r)) (* TODO: stop gap until we have lift from DIV to TAC; actually until we can prove canon_expr in Tot, huh? (see cannon_correct below) *) let canon_expr' (e:expr) : Tot expr = e let pack_fv' (n:name) : term = pack_ln (Tv_FVar (pack_fv n)) let rec expr_to_term (e:expr) : Tot term = match e with | Atom i t -> t | Lit i -> pack_ln (Tv_Const (C_Int i)) | Plus l r -> mk_e_app (pack_fv' add_qn) [expr_to_term l; expr_to_term r] | Minus l r -> mk_e_app (pack_fv' minus_qn) [expr_to_term l; expr_to_term r] | Mult l r -> mk_e_app (pack_fv' mult_qn) [expr_to_term l; expr_to_term r] (* <- TODO this has some chance of not round-tripping well since there is also mult'_qn *) | Neg l -> mk_e_app (pack_fv' neg_qn) [expr_to_term l] (* TODO all the ones below also have implicit arguments that have to be *) (* passed too (number of bits); just how am I supposed to know them? *) | Land l r -> mk_e_app (pack_fv' land_qn) [expr_to_term l; expr_to_term r] | Lor l r -> mk_e_app (pack_fv' lor_qn) [expr_to_term l; expr_to_term r] | Lxor l r -> mk_e_app (pack_fv' lxor_qn) [expr_to_term l; expr_to_term r] | Ladd l r -> mk_e_app (pack_fv' land_qn) [expr_to_term l; expr_to_term r] | Lsub l r -> mk_e_app (pack_fv' lsub_qn) [expr_to_term l; expr_to_term r] | Shl l r -> mk_e_app (pack_fv' shiftl_qn) [expr_to_term l; expr_to_term r] | Shr l r -> mk_e_app (pack_fv' shiftr_qn) [expr_to_term l; expr_to_term r] | NatToBv l -> mk_e_app (pack_fv' nat_bv_qn) [expr_to_term l] | Udiv l r -> mk_e_app (pack_fv' udiv_qn) [expr_to_term l; expr_to_term r] | Umod l r -> mk_e_app (pack_fv' umod_qn) [expr_to_term l; expr_to_term r] | MulMod l r -> mk_e_app (pack_fv' shiftr_qn) [expr_to_term l; expr_to_term r] let canon_correct (e:expr) : Lemma (expr_to_term e == expr_to_term (canon_expr' e)) = () // cheating let term_to_expr (t:term) : Tac expr = admit(); (* TODO: patterns are incomplete nonsense *) match run_tm (is_arith_expr t) with | Inr e -> e | Inl _ -> fail "Term is not an arithmetic expression" let canon_term (t:term) : Tac expr = canon_expr' (term_to_expr t) let canon_deep () : Tac unit = norm []; let g = cur_goal () in match term_as_formula g with | Comp c l r -> let el = term_to_expr l in let er = term_to_expr r in grewrite l (expr_to_term (canon_expr' el)); grewrite r (expr_to_term (canon_expr' er)); simpl (); dump "here I am"; admit1(); admit1(); // canon_correct el admit1() // canon_correct er | _ -> idtac() assume val w : int assume val x : int assume val y : int assume val z : int // Testing the canonizer, it should be the only thing needed for this file
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Reflection.V2.Arith.fst.checked", "FStar.Reflection.V2.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Order.fst.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "CanonDeep.fst" }
[ { "abbrev": true, "full_module": "FStar.Order", "short_module": "O" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Reflection.V2.Arith", "short_module": null }, { "abbrev": false, "full_module": "FStar.Reflection.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "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.Tactics.V2.Derived.or_else", "FStar.Tactics.V2.Derived.qed", "FStar.Tactics.V2.Derived.fail", "FStar.Stubs.Tactics.V2.Builtins.dump", "CanonDeep.canon_deep" ]
[]
false
true
false
false
false
let check_canon_deep () =
canon_deep (); or_else qed (fun () -> dump "`canon deep` left the following goals"; fail "")
false
CanonDeep.fst
CanonDeep.canon_term
val canon_term (t: term) : Tac expr
val canon_term (t: term) : Tac expr
let canon_term (t:term) : Tac expr = canon_expr' (term_to_expr t)
{ "file_name": "examples/native_tactics/CanonDeep.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 65, "end_line": 177, "start_col": 0, "start_line": 177 }
(* Copyright 2008-2018 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 CanonDeep open FStar.Tactics.V2 open FStar.Reflection.V2 open FStar.Reflection.V2.Arith open FStar.Mul module O = FStar.Order (* TODO: The implementation from FStar.Tactics.Canon seems *very* naive: - it repeatedly turns terms into exprs, but that's fixed below - more importantly, a lot of work seems to be done repeatedly and the computational complexity seems huge, way over O(n^2) + terms are traversed both bottom-up and top-down, so that's already O(n^2) + canon_point calls itself recursively even on exprs of the same size Q: Is there a good canonicalization algorithm from literature we can use? A purely functional one? *) (* TODO: expr is mixing up int and uint expressions in a very untyped way. How am I supposed to write an expr_to_term function that always produces well-typed terms when types are so badly mixed up? Tempted to roll out a simplified version of expr that only does ints. *) (* This is the pure part of canon_point *) (* TODO: need Tot to state the lemma Nik wanted, but order not easy at all, and I'm not sure it's worth investing time in verifying such a bad algorithm *) let rec canon_point (e:expr) : Dv expr = match e with // Evaluate constants | Plus (Lit a) (Lit b) -> Lit (a + b) | Mult (Lit a) (Lit b) -> Lit (a * b) // Forget about negations | Neg e -> canon_point (Mult (Lit (-1)) e) // Distribute | Mult a (Plus b c) -> let l = canon_point (Mult a b) in let r = canon_point (Mult a c) in canon_point (Plus l r) | Mult (Plus a b) c -> let l = canon_point (Mult a c) in let r = canon_point (Mult b c) in canon_point (Plus l r) // Associate to the left | Mult a (Mult b c) -> let l = canon_point (Mult a b) in let r = canon_point c in canon_point (Mult l r) | Plus a (Plus b c) -> let l = canon_point (Plus a b) in let r = canon_point c in canon_point (Plus l r) | Plus (Plus a b) c -> if O.gt (compare_expr b c) then begin let l = canon_point (Plus a c) in Plus l b end else e | Mult (Mult a b) c -> if O.gt (compare_expr b c) then begin let l = canon_point (Mult a c) in Mult l b end else e | Plus a (Lit 0) -> a | Plus (Lit 0) b -> b | Plus a b -> if O.gt (compare_expr a b) then Plus b a else e | Mult (Lit 0) _ -> Lit 0 | Mult _ (Lit 0) -> Lit 0 | Mult (Lit 1) r -> r | Mult l (Lit 1) -> l | Mult a b -> if O.gt (compare_expr a b) then Mult b a else e // Forget about subtraction | Minus a b -> let r = canon_point (Neg b) in canon_point (Plus a r) | _ -> e (* This is trying to emulate `pointwise canon_point` *) let rec canon_expr (e:expr) : Dv expr = match e with | Atom _ _ | Lit _ -> e | Plus l r -> canon_point (Plus (canon_expr l) (canon_expr r)) | Minus l r -> canon_point (Minus (canon_expr l) (canon_expr r)) | Mult l r -> canon_point (Mult (canon_expr l) (canon_expr r)) | Neg l -> canon_point (Neg (canon_expr l)) | Land l r -> canon_point (Land (canon_expr l) (canon_expr r)) | Lor l r -> canon_point (Lor (canon_expr l) (canon_expr r)) | Lxor l r -> canon_point (Lxor (canon_expr l) (canon_expr r)) | Ladd l r -> canon_point (Ladd (canon_expr l) (canon_expr r)) | Lsub l r -> canon_point (Lsub (canon_expr l) (canon_expr r)) | Shl l r -> canon_point (Shl (canon_expr l) (canon_expr r)) | Shr l r -> canon_point (Shr (canon_expr l) (canon_expr r)) | NatToBv l -> canon_point (NatToBv (canon_expr l)) | Udiv l r -> canon_point (Udiv (canon_expr l) (canon_expr r)) | Umod l r -> canon_point (Umod (canon_expr l) (canon_expr r)) | MulMod l r -> canon_point (MulMod (canon_expr l) (canon_expr r)) (* TODO: stop gap until we have lift from DIV to TAC; actually until we can prove canon_expr in Tot, huh? (see cannon_correct below) *) let canon_expr' (e:expr) : Tot expr = e let pack_fv' (n:name) : term = pack_ln (Tv_FVar (pack_fv n)) let rec expr_to_term (e:expr) : Tot term = match e with | Atom i t -> t | Lit i -> pack_ln (Tv_Const (C_Int i)) | Plus l r -> mk_e_app (pack_fv' add_qn) [expr_to_term l; expr_to_term r] | Minus l r -> mk_e_app (pack_fv' minus_qn) [expr_to_term l; expr_to_term r] | Mult l r -> mk_e_app (pack_fv' mult_qn) [expr_to_term l; expr_to_term r] (* <- TODO this has some chance of not round-tripping well since there is also mult'_qn *) | Neg l -> mk_e_app (pack_fv' neg_qn) [expr_to_term l] (* TODO all the ones below also have implicit arguments that have to be *) (* passed too (number of bits); just how am I supposed to know them? *) | Land l r -> mk_e_app (pack_fv' land_qn) [expr_to_term l; expr_to_term r] | Lor l r -> mk_e_app (pack_fv' lor_qn) [expr_to_term l; expr_to_term r] | Lxor l r -> mk_e_app (pack_fv' lxor_qn) [expr_to_term l; expr_to_term r] | Ladd l r -> mk_e_app (pack_fv' land_qn) [expr_to_term l; expr_to_term r] | Lsub l r -> mk_e_app (pack_fv' lsub_qn) [expr_to_term l; expr_to_term r] | Shl l r -> mk_e_app (pack_fv' shiftl_qn) [expr_to_term l; expr_to_term r] | Shr l r -> mk_e_app (pack_fv' shiftr_qn) [expr_to_term l; expr_to_term r] | NatToBv l -> mk_e_app (pack_fv' nat_bv_qn) [expr_to_term l] | Udiv l r -> mk_e_app (pack_fv' udiv_qn) [expr_to_term l; expr_to_term r] | Umod l r -> mk_e_app (pack_fv' umod_qn) [expr_to_term l; expr_to_term r] | MulMod l r -> mk_e_app (pack_fv' shiftr_qn) [expr_to_term l; expr_to_term r] let canon_correct (e:expr) : Lemma (expr_to_term e == expr_to_term (canon_expr' e)) = () // cheating let term_to_expr (t:term) : Tac expr = admit(); (* TODO: patterns are incomplete nonsense *) match run_tm (is_arith_expr t) with | Inr e -> e | Inl _ -> fail "Term is not an arithmetic expression"
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Reflection.V2.Arith.fst.checked", "FStar.Reflection.V2.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Order.fst.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "CanonDeep.fst" }
[ { "abbrev": true, "full_module": "FStar.Order", "short_module": "O" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Reflection.V2.Arith", "short_module": null }, { "abbrev": false, "full_module": "FStar.Reflection.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "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.Stubs.Reflection.Types.term -> FStar.Tactics.Effect.Tac FStar.Reflection.V2.Arith.expr
FStar.Tactics.Effect.Tac
[]
[]
[ "FStar.Stubs.Reflection.Types.term", "CanonDeep.canon_expr'", "FStar.Reflection.V2.Arith.expr", "CanonDeep.term_to_expr" ]
[]
false
true
false
false
false
let canon_term (t: term) : Tac expr =
canon_expr' (term_to_expr t)
false
FStar.Pointer.Derived2.fst
FStar.Pointer.Derived2.copy_buffer_contents_precond'
val copy_buffer_contents_precond' (#t: typ) (a b: buffer t) (h: HS.mem) : GTot Type0
val copy_buffer_contents_precond' (#t: typ) (a b: buffer t) (h: HS.mem) : GTot Type0
let copy_buffer_contents_precond' (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) : GTot Type0 = buffer_live h b /\ buffer_readable h a /\ buffer_length b == buffer_length a /\ loc_disjoint (loc_buffer a) (loc_buffer b)
{ "file_name": "ulib/legacy/FStar.Pointer.Derived2.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 44, "end_line": 32, "start_col": 0, "start_line": 23 }
(* Copyright 2008-2018 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 FStar.Pointer.Derived2 module HH = FStar.HyperStack module HS = FStar.HyperStack module HST = FStar.HyperStack.ST
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": true, "source_file": "FStar.Pointer.Derived2.fst" }
[ { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HH" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HH" }, { "abbrev": false, "full_module": "FStar.Pointer.Derived1", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer", "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: FStar.Pointer.Base.buffer t -> b: FStar.Pointer.Base.buffer t -> h: FStar.Monotonic.HyperStack.mem -> Prims.GTot Type0
Prims.GTot
[ "sometrivial" ]
[]
[ "FStar.Pointer.Base.typ", "FStar.Pointer.Base.buffer", "FStar.Monotonic.HyperStack.mem", "Prims.l_and", "FStar.Pointer.Base.buffer_live", "FStar.Pointer.Base.buffer_readable", "Prims.eq2", "FStar.UInt32.t", "FStar.Pointer.Base.buffer_length", "FStar.Pointer.Base.loc_disjoint", "FStar.Pointer.Base.loc_buffer" ]
[]
false
false
false
false
true
let copy_buffer_contents_precond' (#t: typ) (a b: buffer t) (h: HS.mem) : GTot Type0 =
buffer_live h b /\ buffer_readable h a /\ buffer_length b == buffer_length a /\ loc_disjoint (loc_buffer a) (loc_buffer b)
false
CanonDeep.fst
CanonDeep.canon_deep
val canon_deep: Prims.unit -> Tac unit
val canon_deep: Prims.unit -> Tac unit
let canon_deep () : Tac unit = norm []; let g = cur_goal () in match term_as_formula g with | Comp c l r -> let el = term_to_expr l in let er = term_to_expr r in grewrite l (expr_to_term (canon_expr' el)); grewrite r (expr_to_term (canon_expr' er)); simpl (); dump "here I am"; admit1(); admit1(); // canon_correct el admit1() // canon_correct er | _ -> idtac()
{ "file_name": "examples/native_tactics/CanonDeep.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 16, "end_line": 190, "start_col": 0, "start_line": 179 }
(* Copyright 2008-2018 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 CanonDeep open FStar.Tactics.V2 open FStar.Reflection.V2 open FStar.Reflection.V2.Arith open FStar.Mul module O = FStar.Order (* TODO: The implementation from FStar.Tactics.Canon seems *very* naive: - it repeatedly turns terms into exprs, but that's fixed below - more importantly, a lot of work seems to be done repeatedly and the computational complexity seems huge, way over O(n^2) + terms are traversed both bottom-up and top-down, so that's already O(n^2) + canon_point calls itself recursively even on exprs of the same size Q: Is there a good canonicalization algorithm from literature we can use? A purely functional one? *) (* TODO: expr is mixing up int and uint expressions in a very untyped way. How am I supposed to write an expr_to_term function that always produces well-typed terms when types are so badly mixed up? Tempted to roll out a simplified version of expr that only does ints. *) (* This is the pure part of canon_point *) (* TODO: need Tot to state the lemma Nik wanted, but order not easy at all, and I'm not sure it's worth investing time in verifying such a bad algorithm *) let rec canon_point (e:expr) : Dv expr = match e with // Evaluate constants | Plus (Lit a) (Lit b) -> Lit (a + b) | Mult (Lit a) (Lit b) -> Lit (a * b) // Forget about negations | Neg e -> canon_point (Mult (Lit (-1)) e) // Distribute | Mult a (Plus b c) -> let l = canon_point (Mult a b) in let r = canon_point (Mult a c) in canon_point (Plus l r) | Mult (Plus a b) c -> let l = canon_point (Mult a c) in let r = canon_point (Mult b c) in canon_point (Plus l r) // Associate to the left | Mult a (Mult b c) -> let l = canon_point (Mult a b) in let r = canon_point c in canon_point (Mult l r) | Plus a (Plus b c) -> let l = canon_point (Plus a b) in let r = canon_point c in canon_point (Plus l r) | Plus (Plus a b) c -> if O.gt (compare_expr b c) then begin let l = canon_point (Plus a c) in Plus l b end else e | Mult (Mult a b) c -> if O.gt (compare_expr b c) then begin let l = canon_point (Mult a c) in Mult l b end else e | Plus a (Lit 0) -> a | Plus (Lit 0) b -> b | Plus a b -> if O.gt (compare_expr a b) then Plus b a else e | Mult (Lit 0) _ -> Lit 0 | Mult _ (Lit 0) -> Lit 0 | Mult (Lit 1) r -> r | Mult l (Lit 1) -> l | Mult a b -> if O.gt (compare_expr a b) then Mult b a else e // Forget about subtraction | Minus a b -> let r = canon_point (Neg b) in canon_point (Plus a r) | _ -> e (* This is trying to emulate `pointwise canon_point` *) let rec canon_expr (e:expr) : Dv expr = match e with | Atom _ _ | Lit _ -> e | Plus l r -> canon_point (Plus (canon_expr l) (canon_expr r)) | Minus l r -> canon_point (Minus (canon_expr l) (canon_expr r)) | Mult l r -> canon_point (Mult (canon_expr l) (canon_expr r)) | Neg l -> canon_point (Neg (canon_expr l)) | Land l r -> canon_point (Land (canon_expr l) (canon_expr r)) | Lor l r -> canon_point (Lor (canon_expr l) (canon_expr r)) | Lxor l r -> canon_point (Lxor (canon_expr l) (canon_expr r)) | Ladd l r -> canon_point (Ladd (canon_expr l) (canon_expr r)) | Lsub l r -> canon_point (Lsub (canon_expr l) (canon_expr r)) | Shl l r -> canon_point (Shl (canon_expr l) (canon_expr r)) | Shr l r -> canon_point (Shr (canon_expr l) (canon_expr r)) | NatToBv l -> canon_point (NatToBv (canon_expr l)) | Udiv l r -> canon_point (Udiv (canon_expr l) (canon_expr r)) | Umod l r -> canon_point (Umod (canon_expr l) (canon_expr r)) | MulMod l r -> canon_point (MulMod (canon_expr l) (canon_expr r)) (* TODO: stop gap until we have lift from DIV to TAC; actually until we can prove canon_expr in Tot, huh? (see cannon_correct below) *) let canon_expr' (e:expr) : Tot expr = e let pack_fv' (n:name) : term = pack_ln (Tv_FVar (pack_fv n)) let rec expr_to_term (e:expr) : Tot term = match e with | Atom i t -> t | Lit i -> pack_ln (Tv_Const (C_Int i)) | Plus l r -> mk_e_app (pack_fv' add_qn) [expr_to_term l; expr_to_term r] | Minus l r -> mk_e_app (pack_fv' minus_qn) [expr_to_term l; expr_to_term r] | Mult l r -> mk_e_app (pack_fv' mult_qn) [expr_to_term l; expr_to_term r] (* <- TODO this has some chance of not round-tripping well since there is also mult'_qn *) | Neg l -> mk_e_app (pack_fv' neg_qn) [expr_to_term l] (* TODO all the ones below also have implicit arguments that have to be *) (* passed too (number of bits); just how am I supposed to know them? *) | Land l r -> mk_e_app (pack_fv' land_qn) [expr_to_term l; expr_to_term r] | Lor l r -> mk_e_app (pack_fv' lor_qn) [expr_to_term l; expr_to_term r] | Lxor l r -> mk_e_app (pack_fv' lxor_qn) [expr_to_term l; expr_to_term r] | Ladd l r -> mk_e_app (pack_fv' land_qn) [expr_to_term l; expr_to_term r] | Lsub l r -> mk_e_app (pack_fv' lsub_qn) [expr_to_term l; expr_to_term r] | Shl l r -> mk_e_app (pack_fv' shiftl_qn) [expr_to_term l; expr_to_term r] | Shr l r -> mk_e_app (pack_fv' shiftr_qn) [expr_to_term l; expr_to_term r] | NatToBv l -> mk_e_app (pack_fv' nat_bv_qn) [expr_to_term l] | Udiv l r -> mk_e_app (pack_fv' udiv_qn) [expr_to_term l; expr_to_term r] | Umod l r -> mk_e_app (pack_fv' umod_qn) [expr_to_term l; expr_to_term r] | MulMod l r -> mk_e_app (pack_fv' shiftr_qn) [expr_to_term l; expr_to_term r] let canon_correct (e:expr) : Lemma (expr_to_term e == expr_to_term (canon_expr' e)) = () // cheating let term_to_expr (t:term) : Tac expr = admit(); (* TODO: patterns are incomplete nonsense *) match run_tm (is_arith_expr t) with | Inr e -> e | Inl _ -> fail "Term is not an arithmetic expression" let canon_term (t:term) : Tac expr = canon_expr' (term_to_expr t)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Reflection.V2.Arith.fst.checked", "FStar.Reflection.V2.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Order.fst.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "CanonDeep.fst" }
[ { "abbrev": true, "full_module": "FStar.Order", "short_module": "O" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Reflection.V2.Arith", "short_module": null }, { "abbrev": false, "full_module": "FStar.Reflection.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "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.Reflection.V2.Formula.comparison", "FStar.Tactics.NamedView.term", "FStar.Tactics.V2.Derived.admit1", "FStar.Stubs.Tactics.V2.Builtins.dump", "FStar.Tactics.V2.Derived.simpl", "FStar.Tactics.V2.Derived.grewrite", "CanonDeep.expr_to_term", "CanonDeep.canon_expr'", "FStar.Reflection.V2.Arith.expr", "CanonDeep.term_to_expr", "FStar.Reflection.V2.Formula.formula", "FStar.Tactics.V2.Derived.idtac", "FStar.Reflection.V2.Formula.term_as_formula", "FStar.Stubs.Reflection.Types.typ", "FStar.Tactics.V2.Derived.cur_goal", "FStar.Stubs.Tactics.V2.Builtins.norm", "Prims.Nil", "FStar.Pervasives.norm_step" ]
[]
false
true
false
false
false
let canon_deep () : Tac unit =
norm []; let g = cur_goal () in match term_as_formula g with | Comp c l r -> let el = term_to_expr l in let er = term_to_expr r in grewrite l (expr_to_term (canon_expr' el)); grewrite r (expr_to_term (canon_expr' er)); simpl (); dump "here I am"; admit1 (); admit1 (); admit1 () | _ -> idtac ()
false
FStar.Pointer.Derived2.fst
FStar.Pointer.Derived2.copy_buffer_contents_postcond'
val copy_buffer_contents_postcond' (#t: typ) (a b: buffer t) (h h': HS.mem) : GTot Type0
val copy_buffer_contents_postcond' (#t: typ) (a b: buffer t) (h h': HS.mem) : GTot Type0
let copy_buffer_contents_postcond' (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) (h' : HS.mem) : GTot Type0 = copy_buffer_contents_precond' a b h /\ modifies (loc_buffer b) h h' /\ buffer_readable h' b /\ buffer_as_seq h' b == buffer_as_seq h a
{ "file_name": "ulib/legacy/FStar.Pointer.Derived2.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 41, "end_line": 45, "start_col": 0, "start_line": 35 }
(* Copyright 2008-2018 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 FStar.Pointer.Derived2 module HH = FStar.HyperStack module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let copy_buffer_contents_precond' (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) : GTot Type0 = buffer_live h b /\ buffer_readable h a /\ buffer_length b == buffer_length a /\ loc_disjoint (loc_buffer a) (loc_buffer b)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": true, "source_file": "FStar.Pointer.Derived2.fst" }
[ { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HH" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HH" }, { "abbrev": false, "full_module": "FStar.Pointer.Derived1", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer", "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: FStar.Pointer.Base.buffer t -> b: FStar.Pointer.Base.buffer t -> h: FStar.Monotonic.HyperStack.mem -> h': FStar.Monotonic.HyperStack.mem -> Prims.GTot Type0
Prims.GTot
[ "sometrivial" ]
[]
[ "FStar.Pointer.Base.typ", "FStar.Pointer.Base.buffer", "FStar.Monotonic.HyperStack.mem", "Prims.l_and", "FStar.Pointer.Derived2.copy_buffer_contents_precond'", "FStar.Pointer.Base.modifies", "FStar.Pointer.Base.loc_buffer", "FStar.Pointer.Base.buffer_readable", "Prims.eq2", "FStar.Seq.Base.seq", "FStar.Pointer.Base.type_of_typ", "FStar.Pointer.Base.buffer_as_seq" ]
[]
false
false
false
false
true
let copy_buffer_contents_postcond' (#t: typ) (a b: buffer t) (h h': HS.mem) : GTot Type0 =
copy_buffer_contents_precond' a b h /\ modifies (loc_buffer b) h h' /\ buffer_readable h' b /\ buffer_as_seq h' b == buffer_as_seq h a
false
CanonDeep.fst
CanonDeep.lem0
val lem0 : Prims.unit
let lem0 = assert (x * (y * z) == (x * y) * z) by check_canon_deep ()
{ "file_name": "examples/native_tactics/CanonDeep.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 70, "end_line": 205, "start_col": 0, "start_line": 205 }
(* Copyright 2008-2018 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 CanonDeep open FStar.Tactics.V2 open FStar.Reflection.V2 open FStar.Reflection.V2.Arith open FStar.Mul module O = FStar.Order (* TODO: The implementation from FStar.Tactics.Canon seems *very* naive: - it repeatedly turns terms into exprs, but that's fixed below - more importantly, a lot of work seems to be done repeatedly and the computational complexity seems huge, way over O(n^2) + terms are traversed both bottom-up and top-down, so that's already O(n^2) + canon_point calls itself recursively even on exprs of the same size Q: Is there a good canonicalization algorithm from literature we can use? A purely functional one? *) (* TODO: expr is mixing up int and uint expressions in a very untyped way. How am I supposed to write an expr_to_term function that always produces well-typed terms when types are so badly mixed up? Tempted to roll out a simplified version of expr that only does ints. *) (* This is the pure part of canon_point *) (* TODO: need Tot to state the lemma Nik wanted, but order not easy at all, and I'm not sure it's worth investing time in verifying such a bad algorithm *) let rec canon_point (e:expr) : Dv expr = match e with // Evaluate constants | Plus (Lit a) (Lit b) -> Lit (a + b) | Mult (Lit a) (Lit b) -> Lit (a * b) // Forget about negations | Neg e -> canon_point (Mult (Lit (-1)) e) // Distribute | Mult a (Plus b c) -> let l = canon_point (Mult a b) in let r = canon_point (Mult a c) in canon_point (Plus l r) | Mult (Plus a b) c -> let l = canon_point (Mult a c) in let r = canon_point (Mult b c) in canon_point (Plus l r) // Associate to the left | Mult a (Mult b c) -> let l = canon_point (Mult a b) in let r = canon_point c in canon_point (Mult l r) | Plus a (Plus b c) -> let l = canon_point (Plus a b) in let r = canon_point c in canon_point (Plus l r) | Plus (Plus a b) c -> if O.gt (compare_expr b c) then begin let l = canon_point (Plus a c) in Plus l b end else e | Mult (Mult a b) c -> if O.gt (compare_expr b c) then begin let l = canon_point (Mult a c) in Mult l b end else e | Plus a (Lit 0) -> a | Plus (Lit 0) b -> b | Plus a b -> if O.gt (compare_expr a b) then Plus b a else e | Mult (Lit 0) _ -> Lit 0 | Mult _ (Lit 0) -> Lit 0 | Mult (Lit 1) r -> r | Mult l (Lit 1) -> l | Mult a b -> if O.gt (compare_expr a b) then Mult b a else e // Forget about subtraction | Minus a b -> let r = canon_point (Neg b) in canon_point (Plus a r) | _ -> e (* This is trying to emulate `pointwise canon_point` *) let rec canon_expr (e:expr) : Dv expr = match e with | Atom _ _ | Lit _ -> e | Plus l r -> canon_point (Plus (canon_expr l) (canon_expr r)) | Minus l r -> canon_point (Minus (canon_expr l) (canon_expr r)) | Mult l r -> canon_point (Mult (canon_expr l) (canon_expr r)) | Neg l -> canon_point (Neg (canon_expr l)) | Land l r -> canon_point (Land (canon_expr l) (canon_expr r)) | Lor l r -> canon_point (Lor (canon_expr l) (canon_expr r)) | Lxor l r -> canon_point (Lxor (canon_expr l) (canon_expr r)) | Ladd l r -> canon_point (Ladd (canon_expr l) (canon_expr r)) | Lsub l r -> canon_point (Lsub (canon_expr l) (canon_expr r)) | Shl l r -> canon_point (Shl (canon_expr l) (canon_expr r)) | Shr l r -> canon_point (Shr (canon_expr l) (canon_expr r)) | NatToBv l -> canon_point (NatToBv (canon_expr l)) | Udiv l r -> canon_point (Udiv (canon_expr l) (canon_expr r)) | Umod l r -> canon_point (Umod (canon_expr l) (canon_expr r)) | MulMod l r -> canon_point (MulMod (canon_expr l) (canon_expr r)) (* TODO: stop gap until we have lift from DIV to TAC; actually until we can prove canon_expr in Tot, huh? (see cannon_correct below) *) let canon_expr' (e:expr) : Tot expr = e let pack_fv' (n:name) : term = pack_ln (Tv_FVar (pack_fv n)) let rec expr_to_term (e:expr) : Tot term = match e with | Atom i t -> t | Lit i -> pack_ln (Tv_Const (C_Int i)) | Plus l r -> mk_e_app (pack_fv' add_qn) [expr_to_term l; expr_to_term r] | Minus l r -> mk_e_app (pack_fv' minus_qn) [expr_to_term l; expr_to_term r] | Mult l r -> mk_e_app (pack_fv' mult_qn) [expr_to_term l; expr_to_term r] (* <- TODO this has some chance of not round-tripping well since there is also mult'_qn *) | Neg l -> mk_e_app (pack_fv' neg_qn) [expr_to_term l] (* TODO all the ones below also have implicit arguments that have to be *) (* passed too (number of bits); just how am I supposed to know them? *) | Land l r -> mk_e_app (pack_fv' land_qn) [expr_to_term l; expr_to_term r] | Lor l r -> mk_e_app (pack_fv' lor_qn) [expr_to_term l; expr_to_term r] | Lxor l r -> mk_e_app (pack_fv' lxor_qn) [expr_to_term l; expr_to_term r] | Ladd l r -> mk_e_app (pack_fv' land_qn) [expr_to_term l; expr_to_term r] | Lsub l r -> mk_e_app (pack_fv' lsub_qn) [expr_to_term l; expr_to_term r] | Shl l r -> mk_e_app (pack_fv' shiftl_qn) [expr_to_term l; expr_to_term r] | Shr l r -> mk_e_app (pack_fv' shiftr_qn) [expr_to_term l; expr_to_term r] | NatToBv l -> mk_e_app (pack_fv' nat_bv_qn) [expr_to_term l] | Udiv l r -> mk_e_app (pack_fv' udiv_qn) [expr_to_term l; expr_to_term r] | Umod l r -> mk_e_app (pack_fv' umod_qn) [expr_to_term l; expr_to_term r] | MulMod l r -> mk_e_app (pack_fv' shiftr_qn) [expr_to_term l; expr_to_term r] let canon_correct (e:expr) : Lemma (expr_to_term e == expr_to_term (canon_expr' e)) = () // cheating let term_to_expr (t:term) : Tac expr = admit(); (* TODO: patterns are incomplete nonsense *) match run_tm (is_arith_expr t) with | Inr e -> e | Inl _ -> fail "Term is not an arithmetic expression" let canon_term (t:term) : Tac expr = canon_expr' (term_to_expr t) let canon_deep () : Tac unit = norm []; let g = cur_goal () in match term_as_formula g with | Comp c l r -> let el = term_to_expr l in let er = term_to_expr r in grewrite l (expr_to_term (canon_expr' el)); grewrite r (expr_to_term (canon_expr' er)); simpl (); dump "here I am"; admit1(); admit1(); // canon_correct el admit1() // canon_correct er | _ -> idtac() assume val w : int assume val x : int assume val y : int assume val z : int // Testing the canonizer, it should be the only thing needed for this file [@@plugin] let check_canon_deep () = canon_deep (); or_else qed (fun () -> dump "`canon deep` left the following goals"; fail "")
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Reflection.V2.Arith.fst.checked", "FStar.Reflection.V2.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Order.fst.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "CanonDeep.fst" }
[ { "abbrev": true, "full_module": "FStar.Order", "short_module": "O" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Reflection.V2.Arith", "short_module": null }, { "abbrev": false, "full_module": "FStar.Reflection.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "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
Prims.Tot
[ "total" ]
[]
[ "FStar.Tactics.Effect.assert_by_tactic", "Prims.eq2", "Prims.int", "FStar.Mul.op_Star", "CanonDeep.x", "CanonDeep.y", "CanonDeep.z", "Prims.unit", "CanonDeep.check_canon_deep" ]
[]
false
false
false
true
false
let lem0 =
FStar.Tactics.Effect.assert_by_tactic (x * (y * z) == (x * y) * z) (fun _ -> (); check_canon_deep ())
false
FStar.Pointer.Derived2.fst
FStar.Pointer.Derived2.copy_buffer_contents
val copy_buffer_contents (#t: typ) (a: buffer t) (* source *) (idx_a: UInt32.t) (b: buffer t) (* destination *) (idx_b: UInt32.t) (len: UInt32.t) : HST.Stack unit (requires (fun h -> copy_buffer_contents_precond a idx_a b idx_b len h )) (ensures (fun h0 _ h1 -> copy_buffer_contents_postcond a idx_a b idx_b len h0 h1 ))
val copy_buffer_contents (#t: typ) (a: buffer t) (* source *) (idx_a: UInt32.t) (b: buffer t) (* destination *) (idx_b: UInt32.t) (len: UInt32.t) : HST.Stack unit (requires (fun h -> copy_buffer_contents_precond a idx_a b idx_b len h )) (ensures (fun h0 _ h1 -> copy_buffer_contents_postcond a idx_a b idx_b len h0 h1 ))
let copy_buffer_contents = copy_buffer_contents'
{ "file_name": "ulib/legacy/FStar.Pointer.Derived2.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 48, "end_line": 174, "start_col": 0, "start_line": 174 }
(* Copyright 2008-2018 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 FStar.Pointer.Derived2 module HH = FStar.HyperStack module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let copy_buffer_contents_precond' (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) : GTot Type0 = buffer_live h b /\ buffer_readable h a /\ buffer_length b == buffer_length a /\ loc_disjoint (loc_buffer a) (loc_buffer b) private let copy_buffer_contents_postcond' (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) (h' : HS.mem) : GTot Type0 = copy_buffer_contents_precond' a b h /\ modifies (loc_buffer b) h h' /\ buffer_readable h' b /\ buffer_as_seq h' b == buffer_as_seq h a private let copy_buffer_contents_inv (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (len' : UInt32.t) (h: HS.mem) (h' : HS.mem) : GTot Type0 = copy_buffer_contents_precond' a b h /\ modifies (loc_buffer b) h h' /\ UInt32.v len' <= UInt32.v (buffer_length a) /\ buffer_readable h' (gsub_buffer b 0ul len') /\ buffer_as_seq h' (gsub_buffer b 0ul len') == buffer_as_seq h (gsub_buffer a 0ul len') private val copy_buffer_contents_init (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) : Lemma (requires (copy_buffer_contents_precond' a b h)) (ensures (copy_buffer_contents_inv a b 0ul h h)) let copy_buffer_contents_init #t a b h = buffer_readable_intro_empty h (gsub_buffer b 0ul 0ul); Seq.lemma_eq_intro (buffer_as_seq h (gsub_buffer b 0ul 0ul)) (buffer_as_seq h (gsub_buffer a 0ul 0ul)) private val copy_buffer_contents_advance (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (len' : UInt32.t) (h: Ghost.erased HS.mem) : HST.Stack unit (requires (fun h0 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h0 /\ UInt32.v len' < UInt32.v (buffer_length a) )) (ensures (fun h1 _ h2 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h1 /\ UInt32.v len' < UInt32.v (buffer_length a) /\ copy_buffer_contents_inv a b (UInt32.add len' 1ul) (Ghost.reveal h) h2 )) #set-options "--z3rlimit 16" let copy_buffer_contents_advance #t a b len' h = let v = read_buffer a len' in buffer_snoc b 0ul len' v; buffer_as_seq_gsub_buffer_snoc (Ghost.reveal h) a 0ul len' private val copy_buffer_contents_aux (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (len: UInt32.t) (len': UInt32.t) (h: Ghost.erased HS.mem) : HST.Stack unit (requires (fun h0 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h0 /\ len == buffer_length a )) (ensures (fun h0 _ h1 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h0 /\ copy_buffer_contents_postcond' a b (Ghost.reveal h) h1 )) (decreases (UInt32.v (buffer_length a) - UInt32.v len')) let rec copy_buffer_contents_aux #t a b len len' h = if len = len' then () else begin copy_buffer_contents_advance a b len' h; copy_buffer_contents_aux a b len (UInt32.add len' 1ul) h end let copy_buffer_contents_fin (#t: typ) (a: buffer t) (* source *) (idx_a: UInt32.t) (b: buffer t) (* destination *) (idx_b: UInt32.t) (len: UInt32.t) (h: HS.mem) (h' : HS.mem) : Lemma (requires ( copy_buffer_contents_precond a idx_a b idx_b len h /\ copy_buffer_contents_postcond' (gsub_buffer a idx_a len) (gsub_buffer b idx_b len) h h' )) (ensures ( copy_buffer_contents_precond a idx_a b idx_b len h /\ copy_buffer_contents_postcond a idx_a b idx_b len h h' )) = () (* FIXME: Does not work if I directly try to define copy_buffer_contents *) (* FIXME: Works in batch mode (even with --record_hints --use_hints --detail_hint_replay --query_stats) but fails in interactive mode *) let copy_buffer_contents' (#t: typ) (a: buffer t) (* source *) (idx_a: UInt32.t) (b: buffer t) (* destination *) (idx_b: UInt32.t) (len: UInt32.t) : HST.Stack unit (requires (fun h -> copy_buffer_contents_precond a idx_a b idx_b len h )) (ensures (fun h0 _ h1 -> copy_buffer_contents_postcond a idx_a b idx_b len h0 h1 )) = let h0 = HST.get () in let a' = sub_buffer a idx_a len in let b' = sub_buffer b idx_b len in copy_buffer_contents_init a' b' h0; copy_buffer_contents_aux a' b' len 0ul (Ghost.hide h0); let h1 = HST.get () in copy_buffer_contents_fin a idx_a b idx_b len h0 h1
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": true, "source_file": "FStar.Pointer.Derived2.fst" }
[ { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HH" }, { "abbrev": false, "full_module": "FStar.Pointer.Derived1", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer", "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": 16, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.Pointer.Base.buffer t -> idx_a: FStar.UInt32.t -> b: FStar.Pointer.Base.buffer t -> idx_b: FStar.UInt32.t -> len: FStar.UInt32.t -> FStar.HyperStack.ST.Stack Prims.unit
FStar.HyperStack.ST.Stack
[]
[]
[ "FStar.Pointer.Derived2.copy_buffer_contents'" ]
[]
false
true
false
false
false
let copy_buffer_contents =
copy_buffer_contents'
false
CanonDeep.fst
CanonDeep.expr_to_term
val expr_to_term (e: expr) : Tot term
val expr_to_term (e: expr) : Tot term
let rec expr_to_term (e:expr) : Tot term = match e with | Atom i t -> t | Lit i -> pack_ln (Tv_Const (C_Int i)) | Plus l r -> mk_e_app (pack_fv' add_qn) [expr_to_term l; expr_to_term r] | Minus l r -> mk_e_app (pack_fv' minus_qn) [expr_to_term l; expr_to_term r] | Mult l r -> mk_e_app (pack_fv' mult_qn) [expr_to_term l; expr_to_term r] (* <- TODO this has some chance of not round-tripping well since there is also mult'_qn *) | Neg l -> mk_e_app (pack_fv' neg_qn) [expr_to_term l] (* TODO all the ones below also have implicit arguments that have to be *) (* passed too (number of bits); just how am I supposed to know them? *) | Land l r -> mk_e_app (pack_fv' land_qn) [expr_to_term l; expr_to_term r] | Lor l r -> mk_e_app (pack_fv' lor_qn) [expr_to_term l; expr_to_term r] | Lxor l r -> mk_e_app (pack_fv' lxor_qn) [expr_to_term l; expr_to_term r] | Ladd l r -> mk_e_app (pack_fv' land_qn) [expr_to_term l; expr_to_term r] | Lsub l r -> mk_e_app (pack_fv' lsub_qn) [expr_to_term l; expr_to_term r] | Shl l r -> mk_e_app (pack_fv' shiftl_qn) [expr_to_term l; expr_to_term r] | Shr l r -> mk_e_app (pack_fv' shiftr_qn) [expr_to_term l; expr_to_term r] | NatToBv l -> mk_e_app (pack_fv' nat_bv_qn) [expr_to_term l] | Udiv l r -> mk_e_app (pack_fv' udiv_qn) [expr_to_term l; expr_to_term r] | Umod l r -> mk_e_app (pack_fv' umod_qn) [expr_to_term l; expr_to_term r] | MulMod l r -> mk_e_app (pack_fv' shiftr_qn) [expr_to_term l; expr_to_term r]
{ "file_name": "examples/native_tactics/CanonDeep.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 80, "end_line": 166, "start_col": 0, "start_line": 144 }
(* Copyright 2008-2018 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 CanonDeep open FStar.Tactics.V2 open FStar.Reflection.V2 open FStar.Reflection.V2.Arith open FStar.Mul module O = FStar.Order (* TODO: The implementation from FStar.Tactics.Canon seems *very* naive: - it repeatedly turns terms into exprs, but that's fixed below - more importantly, a lot of work seems to be done repeatedly and the computational complexity seems huge, way over O(n^2) + terms are traversed both bottom-up and top-down, so that's already O(n^2) + canon_point calls itself recursively even on exprs of the same size Q: Is there a good canonicalization algorithm from literature we can use? A purely functional one? *) (* TODO: expr is mixing up int and uint expressions in a very untyped way. How am I supposed to write an expr_to_term function that always produces well-typed terms when types are so badly mixed up? Tempted to roll out a simplified version of expr that only does ints. *) (* This is the pure part of canon_point *) (* TODO: need Tot to state the lemma Nik wanted, but order not easy at all, and I'm not sure it's worth investing time in verifying such a bad algorithm *) let rec canon_point (e:expr) : Dv expr = match e with // Evaluate constants | Plus (Lit a) (Lit b) -> Lit (a + b) | Mult (Lit a) (Lit b) -> Lit (a * b) // Forget about negations | Neg e -> canon_point (Mult (Lit (-1)) e) // Distribute | Mult a (Plus b c) -> let l = canon_point (Mult a b) in let r = canon_point (Mult a c) in canon_point (Plus l r) | Mult (Plus a b) c -> let l = canon_point (Mult a c) in let r = canon_point (Mult b c) in canon_point (Plus l r) // Associate to the left | Mult a (Mult b c) -> let l = canon_point (Mult a b) in let r = canon_point c in canon_point (Mult l r) | Plus a (Plus b c) -> let l = canon_point (Plus a b) in let r = canon_point c in canon_point (Plus l r) | Plus (Plus a b) c -> if O.gt (compare_expr b c) then begin let l = canon_point (Plus a c) in Plus l b end else e | Mult (Mult a b) c -> if O.gt (compare_expr b c) then begin let l = canon_point (Mult a c) in Mult l b end else e | Plus a (Lit 0) -> a | Plus (Lit 0) b -> b | Plus a b -> if O.gt (compare_expr a b) then Plus b a else e | Mult (Lit 0) _ -> Lit 0 | Mult _ (Lit 0) -> Lit 0 | Mult (Lit 1) r -> r | Mult l (Lit 1) -> l | Mult a b -> if O.gt (compare_expr a b) then Mult b a else e // Forget about subtraction | Minus a b -> let r = canon_point (Neg b) in canon_point (Plus a r) | _ -> e (* This is trying to emulate `pointwise canon_point` *) let rec canon_expr (e:expr) : Dv expr = match e with | Atom _ _ | Lit _ -> e | Plus l r -> canon_point (Plus (canon_expr l) (canon_expr r)) | Minus l r -> canon_point (Minus (canon_expr l) (canon_expr r)) | Mult l r -> canon_point (Mult (canon_expr l) (canon_expr r)) | Neg l -> canon_point (Neg (canon_expr l)) | Land l r -> canon_point (Land (canon_expr l) (canon_expr r)) | Lor l r -> canon_point (Lor (canon_expr l) (canon_expr r)) | Lxor l r -> canon_point (Lxor (canon_expr l) (canon_expr r)) | Ladd l r -> canon_point (Ladd (canon_expr l) (canon_expr r)) | Lsub l r -> canon_point (Lsub (canon_expr l) (canon_expr r)) | Shl l r -> canon_point (Shl (canon_expr l) (canon_expr r)) | Shr l r -> canon_point (Shr (canon_expr l) (canon_expr r)) | NatToBv l -> canon_point (NatToBv (canon_expr l)) | Udiv l r -> canon_point (Udiv (canon_expr l) (canon_expr r)) | Umod l r -> canon_point (Umod (canon_expr l) (canon_expr r)) | MulMod l r -> canon_point (MulMod (canon_expr l) (canon_expr r)) (* TODO: stop gap until we have lift from DIV to TAC; actually until we can prove canon_expr in Tot, huh? (see cannon_correct below) *) let canon_expr' (e:expr) : Tot expr = e let pack_fv' (n:name) : term = pack_ln (Tv_FVar (pack_fv n))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Reflection.V2.Arith.fst.checked", "FStar.Reflection.V2.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Order.fst.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "CanonDeep.fst" }
[ { "abbrev": true, "full_module": "FStar.Order", "short_module": "O" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Reflection.V2.Arith", "short_module": null }, { "abbrev": false, "full_module": "FStar.Reflection.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "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: FStar.Reflection.V2.Arith.expr -> FStar.Stubs.Reflection.Types.term
Prims.Tot
[ "total" ]
[]
[ "FStar.Reflection.V2.Arith.expr", "Prims.nat", "FStar.Stubs.Reflection.Types.term", "Prims.int", "FStar.Stubs.Reflection.V2.Builtins.pack_ln", "FStar.Stubs.Reflection.V2.Data.Tv_Const", "FStar.Stubs.Reflection.V2.Data.C_Int", "FStar.Reflection.V2.Derived.mk_e_app", "CanonDeep.pack_fv'", "FStar.Reflection.Const.add_qn", "Prims.Cons", "CanonDeep.expr_to_term", "Prims.Nil", "FStar.Reflection.Const.minus_qn", "FStar.Reflection.Const.mult_qn", "FStar.Reflection.Const.neg_qn", "FStar.Reflection.Const.land_qn", "FStar.Reflection.Const.lor_qn", "FStar.Reflection.Const.lxor_qn", "FStar.Reflection.Const.lsub_qn", "FStar.Reflection.Const.shiftl_qn", "FStar.Reflection.Const.shiftr_qn", "FStar.Reflection.Const.nat_bv_qn", "FStar.Reflection.Const.udiv_qn", "FStar.Reflection.Const.umod_qn" ]
[ "recursion" ]
false
false
false
true
false
let rec expr_to_term (e: expr) : Tot term =
match e with | Atom i t -> t | Lit i -> pack_ln (Tv_Const (C_Int i)) | Plus l r -> mk_e_app (pack_fv' add_qn) [expr_to_term l; expr_to_term r] | Minus l r -> mk_e_app (pack_fv' minus_qn) [expr_to_term l; expr_to_term r] | Mult l r -> mk_e_app (pack_fv' mult_qn) [expr_to_term l; expr_to_term r] | Neg l -> mk_e_app (pack_fv' neg_qn) [expr_to_term l] | Land l r -> mk_e_app (pack_fv' land_qn) [expr_to_term l; expr_to_term r] | Lor l r -> mk_e_app (pack_fv' lor_qn) [expr_to_term l; expr_to_term r] | Lxor l r -> mk_e_app (pack_fv' lxor_qn) [expr_to_term l; expr_to_term r] | Ladd l r -> mk_e_app (pack_fv' land_qn) [expr_to_term l; expr_to_term r] | Lsub l r -> mk_e_app (pack_fv' lsub_qn) [expr_to_term l; expr_to_term r] | Shl l r -> mk_e_app (pack_fv' shiftl_qn) [expr_to_term l; expr_to_term r] | Shr l r -> mk_e_app (pack_fv' shiftr_qn) [expr_to_term l; expr_to_term r] | NatToBv l -> mk_e_app (pack_fv' nat_bv_qn) [expr_to_term l] | Udiv l r -> mk_e_app (pack_fv' udiv_qn) [expr_to_term l; expr_to_term r] | Umod l r -> mk_e_app (pack_fv' umod_qn) [expr_to_term l; expr_to_term r] | MulMod l r -> mk_e_app (pack_fv' shiftr_qn) [expr_to_term l; expr_to_term r]
false
CanonDeep.fst
CanonDeep.canon_expr
val canon_expr (e: expr) : Dv expr
val canon_expr (e: expr) : Dv expr
let rec canon_expr (e:expr) : Dv expr = match e with | Atom _ _ | Lit _ -> e | Plus l r -> canon_point (Plus (canon_expr l) (canon_expr r)) | Minus l r -> canon_point (Minus (canon_expr l) (canon_expr r)) | Mult l r -> canon_point (Mult (canon_expr l) (canon_expr r)) | Neg l -> canon_point (Neg (canon_expr l)) | Land l r -> canon_point (Land (canon_expr l) (canon_expr r)) | Lor l r -> canon_point (Lor (canon_expr l) (canon_expr r)) | Lxor l r -> canon_point (Lxor (canon_expr l) (canon_expr r)) | Ladd l r -> canon_point (Ladd (canon_expr l) (canon_expr r)) | Lsub l r -> canon_point (Lsub (canon_expr l) (canon_expr r)) | Shl l r -> canon_point (Shl (canon_expr l) (canon_expr r)) | Shr l r -> canon_point (Shr (canon_expr l) (canon_expr r)) | NatToBv l -> canon_point (NatToBv (canon_expr l)) | Udiv l r -> canon_point (Udiv (canon_expr l) (canon_expr r)) | Umod l r -> canon_point (Umod (canon_expr l) (canon_expr r)) | MulMod l r -> canon_point (MulMod (canon_expr l) (canon_expr r))
{ "file_name": "examples/native_tactics/CanonDeep.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 68, "end_line": 135, "start_col": 0, "start_line": 118 }
(* Copyright 2008-2018 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 CanonDeep open FStar.Tactics.V2 open FStar.Reflection.V2 open FStar.Reflection.V2.Arith open FStar.Mul module O = FStar.Order (* TODO: The implementation from FStar.Tactics.Canon seems *very* naive: - it repeatedly turns terms into exprs, but that's fixed below - more importantly, a lot of work seems to be done repeatedly and the computational complexity seems huge, way over O(n^2) + terms are traversed both bottom-up and top-down, so that's already O(n^2) + canon_point calls itself recursively even on exprs of the same size Q: Is there a good canonicalization algorithm from literature we can use? A purely functional one? *) (* TODO: expr is mixing up int and uint expressions in a very untyped way. How am I supposed to write an expr_to_term function that always produces well-typed terms when types are so badly mixed up? Tempted to roll out a simplified version of expr that only does ints. *) (* This is the pure part of canon_point *) (* TODO: need Tot to state the lemma Nik wanted, but order not easy at all, and I'm not sure it's worth investing time in verifying such a bad algorithm *) let rec canon_point (e:expr) : Dv expr = match e with // Evaluate constants | Plus (Lit a) (Lit b) -> Lit (a + b) | Mult (Lit a) (Lit b) -> Lit (a * b) // Forget about negations | Neg e -> canon_point (Mult (Lit (-1)) e) // Distribute | Mult a (Plus b c) -> let l = canon_point (Mult a b) in let r = canon_point (Mult a c) in canon_point (Plus l r) | Mult (Plus a b) c -> let l = canon_point (Mult a c) in let r = canon_point (Mult b c) in canon_point (Plus l r) // Associate to the left | Mult a (Mult b c) -> let l = canon_point (Mult a b) in let r = canon_point c in canon_point (Mult l r) | Plus a (Plus b c) -> let l = canon_point (Plus a b) in let r = canon_point c in canon_point (Plus l r) | Plus (Plus a b) c -> if O.gt (compare_expr b c) then begin let l = canon_point (Plus a c) in Plus l b end else e | Mult (Mult a b) c -> if O.gt (compare_expr b c) then begin let l = canon_point (Mult a c) in Mult l b end else e | Plus a (Lit 0) -> a | Plus (Lit 0) b -> b | Plus a b -> if O.gt (compare_expr a b) then Plus b a else e | Mult (Lit 0) _ -> Lit 0 | Mult _ (Lit 0) -> Lit 0 | Mult (Lit 1) r -> r | Mult l (Lit 1) -> l | Mult a b -> if O.gt (compare_expr a b) then Mult b a else e // Forget about subtraction | Minus a b -> let r = canon_point (Neg b) in canon_point (Plus a r) | _ -> e
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.Tactics.V2.fst.checked", "FStar.Tactics.Effect.fsti.checked", "FStar.Reflection.V2.Arith.fst.checked", "FStar.Reflection.V2.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Order.fst.checked", "FStar.Mul.fst.checked" ], "interface_file": false, "source_file": "CanonDeep.fst" }
[ { "abbrev": true, "full_module": "FStar.Order", "short_module": "O" }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "FStar.Reflection.V2.Arith", "short_module": null }, { "abbrev": false, "full_module": "FStar.Reflection.V2", "short_module": null }, { "abbrev": false, "full_module": "FStar.Tactics.V2", "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: FStar.Reflection.V2.Arith.expr -> FStar.Pervasives.Dv FStar.Reflection.V2.Arith.expr
FStar.Pervasives.Dv
[]
[]
[ "FStar.Reflection.V2.Arith.expr", "Prims.nat", "FStar.Stubs.Reflection.Types.term", "Prims.int", "CanonDeep.canon_point", "FStar.Reflection.V2.Arith.Plus", "CanonDeep.canon_expr", "FStar.Reflection.V2.Arith.Minus", "FStar.Reflection.V2.Arith.Mult", "FStar.Reflection.V2.Arith.Neg", "FStar.Reflection.V2.Arith.Land", "FStar.Reflection.V2.Arith.Lor", "FStar.Reflection.V2.Arith.Lxor", "FStar.Reflection.V2.Arith.Ladd", "FStar.Reflection.V2.Arith.Lsub", "FStar.Reflection.V2.Arith.Shl", "FStar.Reflection.V2.Arith.Shr", "FStar.Reflection.V2.Arith.NatToBv", "FStar.Reflection.V2.Arith.Udiv", "FStar.Reflection.V2.Arith.Umod", "FStar.Reflection.V2.Arith.MulMod" ]
[ "recursion" ]
false
true
false
false
false
let rec canon_expr (e: expr) : Dv expr =
match e with | Atom _ _ | Lit _ -> e | Plus l r -> canon_point (Plus (canon_expr l) (canon_expr r)) | Minus l r -> canon_point (Minus (canon_expr l) (canon_expr r)) | Mult l r -> canon_point (Mult (canon_expr l) (canon_expr r)) | Neg l -> canon_point (Neg (canon_expr l)) | Land l r -> canon_point (Land (canon_expr l) (canon_expr r)) | Lor l r -> canon_point (Lor (canon_expr l) (canon_expr r)) | Lxor l r -> canon_point (Lxor (canon_expr l) (canon_expr r)) | Ladd l r -> canon_point (Ladd (canon_expr l) (canon_expr r)) | Lsub l r -> canon_point (Lsub (canon_expr l) (canon_expr r)) | Shl l r -> canon_point (Shl (canon_expr l) (canon_expr r)) | Shr l r -> canon_point (Shr (canon_expr l) (canon_expr r)) | NatToBv l -> canon_point (NatToBv (canon_expr l)) | Udiv l r -> canon_point (Udiv (canon_expr l) (canon_expr r)) | Umod l r -> canon_point (Umod (canon_expr l) (canon_expr r)) | MulMod l r -> canon_point (MulMod (canon_expr l) (canon_expr r))
false
Steel.Effect.Atomic.fst
Steel.Effect.Atomic.elim_vdep0
val elim_vdep0 (#opened: inames) (v: vprop) (p: (t_of v -> Tot vprop)) (q: vprop) : SteelGhost unit opened (vdep v p) (fun _ -> v `star` q) (requires (fun h -> q == p (dfst (h (vdep v p))))) (ensures (fun h _ h' -> let fs = h' v in let sn = h' q in let x2 = h (vdep v p) in q == p fs /\ dfst x2 == fs /\ dsnd x2 == sn))
val elim_vdep0 (#opened: inames) (v: vprop) (p: (t_of v -> Tot vprop)) (q: vprop) : SteelGhost unit opened (vdep v p) (fun _ -> v `star` q) (requires (fun h -> q == p (dfst (h (vdep v p))))) (ensures (fun h _ h' -> let fs = h' v in let sn = h' q in let x2 = h (vdep v p) in q == p fs /\ dfst x2 == fs /\ dsnd x2 == sn))
let elim_vdep0 (#opened:inames) (v: vprop) (p: (t_of v -> Tot vprop)) (q: vprop) : SteelGhost unit opened (vdep v p) (fun _ -> v `star` q) (requires (fun h -> q == p (dfst (h (vdep v p))))) (ensures (fun h _ h' -> let fs = h' v in let sn = h' q in let x2 = h (vdep v p) in q == p fs /\ dfst x2 == fs /\ dsnd x2 == sn )) = change_slprop_rel_with_cond (vdep v p) (v `star` q) (vdep_cond_recip v p q) (vdep_rel_recip v q p) (fun m -> elim_vdep_lemma v q p m); reveal_star v q
{ "file_name": "lib/steel/Steel.Effect.Atomic.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 17, "end_line": 833, "start_col": 0, "start_line": 810 }
(* 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.Atomic open Steel.Effect friend Steel.Effect #set-options "--warn_error -330" //turn off the experimental feature warning let _ : squash (forall (pre:pre_t) (m0:mem{interp (hp_of pre) m0}) (m1:mem{disjoint m0 m1}). mk_rmem pre m0 == mk_rmem pre (join m0 m1)) = Classical.forall_intro rmem_depends_only_on let req_to_act_req (#pre:vprop) (req:req_t pre) : mprop (hp_of pre) = fun m -> rmem_depends_only_on pre; interp (hp_of pre) m /\ req (mk_rmem pre m) unfold let to_post (#a:Type) (post:post_t a) = fun x -> (hp_of (post x)) let ens_to_act_ens (#pre:pre_t) (#a:Type) (#post:post_t a) (ens:ens_t pre a post) : mprop2 (hp_of pre) (to_post post) = fun m0 x m1 -> interp (hp_of pre) m0 /\ interp (hp_of (post x)) m1 /\ ens (mk_rmem pre m0) x (mk_rmem (post x) m1) let repr a framed opened f pre post req ens = action_except_full a opened (hp_of pre) (to_post post) (req_to_act_req req) (ens_to_act_ens ens) let return_ a x opened #p = fun _ -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (p x) (core_mem m0) in lemma_frame_equalities_refl (p x) h0; x #push-options "--fuel 0 --ifuel 0" #push-options "--z3rlimit 20 --fuel 1 --ifuel 1" val frame00 (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:repr a framed opened obs pre post req ens) (frame:vprop) : repr a true opened obs (pre `star` frame) (fun x -> post x `star` frame) (fun h -> req (focus_rmem h pre)) (fun h0 r h1 -> req (focus_rmem h0 pre) /\ ens (focus_rmem h0 pre) r (focus_rmem h1 (post r)) /\ frame_opaque frame (focus_rmem h0 frame) (focus_rmem h1 frame)) module Sem = Steel.Semantics.Hoare.MST module Mem = Steel.Memory let equiv_middle_left_assoc (a b c d:slprop) : Lemma (((a `Mem.star` b) `Mem.star` c `Mem.star` d) `Mem.equiv` (a `Mem.star` (b `Mem.star` c) `Mem.star` d)) = let open Steel.Memory in star_associative a b c; star_congruence ((a `star` b) `star` c) d (a `star` (b `star` c)) d let frame00 #a #framed #opened #obs #pre #post #req #ens f frame = fun frame' -> let m0:full_mem = NMSTTotal.get () in let snap:rmem frame = mk_rmem frame (core_mem m0) in // Need to define it with type annotation, although unused, for it to trigger // the pattern on the framed ensures in the def of MstTot let aux:mprop (hp_of frame `Mem.star` frame') = req_frame frame snap in focus_is_restrict_mk_rmem (pre `star` frame) pre (core_mem m0); assert (interp (hp_of (pre `star` frame) `Mem.star` frame' `Mem.star` locks_invariant opened m0) m0); equiv_middle_left_assoc (hp_of pre) (hp_of frame) frame' (locks_invariant opened m0); assert (interp (hp_of pre `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m0) m0); let x = f (hp_of frame `Mem.star` frame') in let m1:full_mem = NMSTTotal.get () in assert (interp (hp_of (post x) `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m1) m1); equiv_middle_left_assoc (hp_of (post x)) (hp_of frame) frame' (locks_invariant opened m1); assert (interp ((hp_of (post x) `Mem.star` hp_of frame) `Mem.star` frame' `Mem.star` locks_invariant opened m1) m1); focus_is_restrict_mk_rmem (pre `star` frame) frame (core_mem m0); focus_is_restrict_mk_rmem (post x `star` frame) frame (core_mem m1); let h0:rmem (pre `star` frame) = mk_rmem (pre `star` frame) (core_mem m0) in let h1:rmem (post x `star` frame) = mk_rmem (post x `star` frame) (core_mem m1) in assert (focus_rmem h0 frame == focus_rmem h1 frame); focus_is_restrict_mk_rmem (post x `star` frame) (post x) (core_mem m1); lemma_frame_opaque_refl frame (focus_rmem h0 frame); x unfold let bind_req_opaque (#a:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#pr:a -> prop) (req_g:(x:a -> req_t (pre_g x))) (frame_f:vprop) (frame_g:a -> vprop) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) : req_t (pre_f `star` frame_f) = fun m0 -> req_f (focus_rmem m0 pre_f) /\ (forall (x:a) (h1:hmem (post_f x `star` frame_f)). (ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f)) ==> pr x /\ (can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); (req_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)))) unfold let bind_ens_opaque (#a:Type) (#b:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#pr:a -> prop) (ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (frame_f:vprop) (frame_g:a -> vprop) (post:post_t b) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (_:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) : ens_t (pre_f `star` frame_f) b post = fun m0 y m2 -> req_f (focus_rmem m0 pre_f) /\ (exists (x:a) (h1:hmem (post_f x `star` frame_f)). pr x /\ ( can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f) /\ frame_opaque (frame_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (frame_g x)) (focus_rmem m2 (frame_g x)) /\ ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ (ens_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)) y (focus_rmem m2 (post_g x y)))) val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True) #push-options "--z3rlimit 20" let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_f x `star` frame_f) (core_mem m1) in let h1' = mk_rmem (pre_g x `star` frame_g x) (core_mem m1) in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); focus_is_restrict_mk_rmem (post_f x `star` frame_f) (pre_g x `star` frame_g x) (core_mem m1); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of (post_f x `star` frame_f)) (hp_of (pre_g x `star` frame_g x)) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); let h2' = mk_rmem (post_g x y `star` frame_g x) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in // assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem (post_g x y `star` frame_g x) (post y) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of (post_g x y `star` frame_g x)) (hp_of (post y)) frame (locks_invariant opened m2) m2; y let norm_repr (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) (f:repr a framed opened obs pre post req ens) : repr a framed opened obs pre post (fun h -> norm_opaque (req h)) (fun h0 x h1 -> norm_opaque (ens h0 x h1)) = f let bind a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = norm_repr (bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g) val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True) let subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem pre_g (core_mem m0) in can_be_split_trans pre_g (pre_f `star` fr) pre_f; can_be_split_trans pre_g (pre_f `star` fr) fr; can_be_split_3_interp (hp_of pre_g) (hp_of (pre_f `star` fr)) frame (locks_invariant opened m0) m0; focus_replace pre_g (pre_f `star` fr) pre_f (core_mem m0); let x = frame00 f fr frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_g x) (core_mem m1) in let h0' = mk_rmem (pre_f `star` fr) (core_mem m0) in let h1' = mk_rmem (post_f x `star` fr) (core_mem m1) in // From frame00 assert (frame_opaque fr (focus_rmem h0' fr) (focus_rmem h1' fr)); // Replace h0'/h1' by h0/h1 focus_replace pre_g (pre_f `star` fr) fr (core_mem m0); focus_replace (post_g x) (post_f x `star` fr) fr (core_mem m1); assert (frame_opaque fr (focus_rmem h0 fr) (focus_rmem h1 fr)); can_be_split_trans (post_g x) (post_f x `star` fr) (post_f x); can_be_split_trans (post_g x) (post_f x `star` fr) fr; can_be_split_3_interp (hp_of (post_f x `star` fr)) (hp_of (post_g x)) frame (locks_invariant opened m1) m1; focus_replace (post_g x) (post_f x `star` fr) (post_f x) (core_mem m1); x let subcomp a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #_ #pr #p1 #p2 f = lemma_subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2; subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f #pop-options let bind_pure_steela_ a b opened o #wp f g = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun frame -> let x = f () in g x frame let lift_ghost_atomic a o f = f let lift_atomic_steel a o f = f let as_atomic_action f = SteelAtomic?.reflect f let as_atomic_action_ghost f = SteelGhost?.reflect f let as_atomic_unobservable_action f = SteelAtomicU?.reflect f (* Some helpers *) let get0 (#opened:inames) (#p:vprop) (_:unit) : repr (erased (rmem p)) true opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1 /\ frame_equalities p r h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; h0 let get () = SteelGhost?.reflect (get0 ()) let intro_star (p q:vprop) (r:slprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (m:mem) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m) ) : Lemma (requires interp ((hp_of p) `Mem.star` r) m /\ sel_of p m == reveal vp) (ensures interp ((hp_of q) `Mem.star` r) m) = let p = hp_of p in let q = hp_of q in let intro (ml mr:mem) : Lemma (requires interp q ml /\ interp r mr /\ disjoint ml mr) (ensures disjoint ml mr /\ interp (q `Mem.star` r) (Mem.join ml mr)) = Mem.intro_star q r ml mr in elim_star p r m; Classical.forall_intro (Classical.move_requires proof); Classical.forall_intro_2 (Classical.move_requires_2 intro) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let rewrite_slprop0 (#opened:inames) (p q:vprop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ _ -> True) = fun frame -> let m:full_mem = NMSTTotal.get () in proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) (sel_of q m) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let rewrite_slprop p q l = SteelGhost?.reflect (rewrite_slprop0 p q l) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop0 (#opened:inames) (p q:vprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun h -> h p == reveal vp) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) vp vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop p q vp vq l = SteelGhost?.reflect (change_slprop0 p q vp vq l) let change_equal_slprop p q = let m = get () in let x : Ghost.erased (t_of p) = hide ((reveal m) p) in let y : Ghost.erased (t_of q) = Ghost.hide (Ghost.reveal x) in change_slprop p q x y (fun _ -> ()) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop_20 (#opened:inames) (p q:vprop) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop_2 p q vq l = SteelGhost?.reflect (change_slprop_20 p q vq l) let change_slprop_rel0 (#opened:inames) (p q:vprop) (rel : normal (t_of p) -> normal (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel p q rel proof = SteelGhost?.reflect (change_slprop_rel0 p q rel proof) let change_slprop_rel_with_cond0 (#opened:inames) (p q:vprop) (cond: t_of p -> prop) (rel : (t_of p) -> (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ cond (sel_of p m)) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun m -> cond (m p)) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel_with_cond p q cond rel proof = SteelGhost?.reflect (change_slprop_rel_with_cond0 p q cond rel proof) let extract_info0 (#opened:inames) (p:vprop) (vp:erased (normal (t_of p))) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> h p == reveal vp) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info p vp fact l = SteelGhost?.reflect (extract_info0 p vp fact l) let extract_info_raw0 (#opened:inames) (p:vprop) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> True) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info_raw p fact l = SteelGhost?.reflect (extract_info_raw0 p fact l) let noop _ = change_slprop_rel emp emp (fun _ _ -> True) (fun _ -> ()) let sladmit _ = SteelGhostF?.reflect (fun _ -> NMSTTotal.nmst_tot_admit ()) let slassert0 (#opened:inames) (p:vprop) : repr unit false opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0 let slassert p = SteelGhost?.reflect (slassert0 p) let drop p = rewrite_slprop p emp (fun m -> emp_unit (hp_of p); affine_star (hp_of p) Mem.emp m; reveal_emp()) let reveal_star0 (#opened:inames) (p1 p2:vprop) : repr unit false opened Unobservable (p1 `star` p2) (fun _ -> p1 `star` p2) (fun _ -> True) (fun h0 _ h1 -> h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 (p1 `star` p2) == (h0 p1, h0 p2) /\ h1 (p1 `star` p2) == (h1 p1, h1 p2) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem let reveal_star p1 p2 = SteelGhost?.reflect (reveal_star0 p1 p2) let reveal_star_30 (#opened:inames) (p1 p2 p3:vprop) : repr unit false opened Unobservable (p1 `star` p2 `star` p3) (fun _ -> p1 `star` p2 `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split (p1 `star` p2 `star` p3) p1 /\ can_be_split (p1 `star` p2 `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 (p1 `star` p2 `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 (p1 `star` p2 `star` p3) == ((h1 p1, h1 p2), h1 p3) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem (p1 `star` p2 `star` p3) (core_mem m) in can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p1; can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p2; reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2 `star` p3); reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2); reveal_mk_rmem (p1 `star` p2 `star` p3) m p3 let reveal_star_3 p1 p2 p3 = SteelGhost?.reflect (reveal_star_30 p1 p2 p3) let intro_pure p = rewrite_slprop emp (pure p) (fun m -> pure_interp p m) let elim_pure_aux #uses (p:prop) : SteelGhostT (_:unit{p}) uses (pure p) (fun _ -> to_vprop Mem.emp) = as_atomic_action_ghost (Steel.Memory.elim_pure #uses p) let elim_pure #uses p = let _ = elim_pure_aux p in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()) let return #a #opened #p x = SteelAtomicBase?.reflect (return_ a x opened #p) let intro_exists #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists x (h_exists_sl' p) m) let intro_exists_erased #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists (Ghost.reveal x) (h_exists_sl' p) m) let witness_exists #a #u #p _ = SteelGhost?.reflect (Steel.Memory.witness_h_exists #u (fun x -> hp_of (p x))) let lift_exists #a #u p = as_atomic_action_ghost (Steel.Memory.lift_h_exists #u (fun x -> hp_of (p x))) let exists_equiv p q = Classical.forall_intro_2 reveal_equiv; h_exists_cong (h_exists_sl' p) (h_exists_sl' q) let exists_cong p q = rewrite_slprop (h_exists p) (h_exists q) (fun m -> reveal_equiv (h_exists p) (h_exists q); exists_equiv p q) let fresh_invariant #uses p ctxt = rewrite_slprop p (to_vprop (hp_of p)) (fun _ -> ()); let i = as_atomic_unobservable_action (fresh_invariant uses (hp_of p) ctxt) in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()); return i let new_invariant #uses p = let i = fresh_invariant #uses p [] in return i (* * AR: SteelAtomic and SteelGhost are not marked reifiable since we intend to run Steel programs natively * However to implement the with_inv combinators we need to reify their thunks to reprs * We could implement it better by having support for reification only in the .fst file * But for now assuming a function *) assume val reify_steel_atomic_comp (#a:Type) (#framed:bool) (#opened_invariants:inames) (#g:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:unit -> SteelAtomicBase a framed opened_invariants g pre post req ens) : repr a framed opened_invariants g pre post req ens [@@warn_on_use "as_unobservable_atomic_action is a trusted primitive"] let as_atomic_o_action (#a:Type u#a) (#opened_invariants:inames) (#fp:slprop) (#fp': a -> slprop) (o:observability) (f:action_except a opened_invariants fp fp') : SteelAtomicBaseT a opened_invariants o (to_vprop fp) (fun x -> to_vprop (fp' x)) = SteelAtomicBaseT?.reflect f let with_invariant #a #fp #fp' #obs #opened #p i f = rewrite_slprop fp (to_vprop (hp_of fp)) (fun _ -> ()); let x = as_atomic_o_action obs (Steel.Memory.with_invariant #a #(hp_of fp) #(fun x -> hp_of (fp' x)) #opened #(hp_of p) i (reify_steel_atomic_comp f)) in rewrite_slprop (to_vprop (hp_of (fp' x))) (fp' x) (fun _ -> ()); return x assume val reify_steel_ghost_comp (#a:Type) (#framed:bool) (#opened_invariants:inames) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:unit -> SteelGhostBase a framed opened_invariants Unobservable pre post req ens) : repr a framed opened_invariants Unobservable pre post req ens let with_invariant_g #a #fp #fp' #opened #p i f = rewrite_slprop fp (to_vprop (hp_of fp)) (fun _ -> ()); let x = as_atomic_unobservable_action (Steel.Memory.with_invariant #a #(hp_of fp) #(fun x -> hp_of (fp' x)) #opened #(hp_of p) i (reify_steel_ghost_comp f)) in rewrite_slprop (to_vprop (hp_of (fp' x))) (fp' x) (fun _ -> ()); return (hide x) let intro_vrefine v p = let m = get () in let x : Ghost.erased (t_of v) = gget v in let x' : Ghost.erased (vrefine_t v p) = Ghost.hide (Ghost.reveal x) in change_slprop v (vrefine v p) x x' (fun m -> interp_vrefine_hp v p m; vrefine_sel_eq v p m ) let elim_vrefine v p = let h = get() in let x : Ghost.erased (vrefine_t v p) = gget (vrefine v p) in let x' : Ghost.erased (t_of v) = Ghost.hide (Ghost.reveal x) in change_slprop (vrefine v p) v x x' (fun m -> interp_vrefine_hp v p m; vrefine_sel_eq v p m ) let vdep_cond (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (x1: t_of (v `star` q)) : Tot prop = q == p (fst x1) let vdep_rel (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (x1: t_of (v `star` q)) (x2: (t_of (vdep v p))) : Tot prop = q == p (fst x1) /\ dfst (x2 <: (dtuple2 (t_of v) (vdep_payload v p))) == fst x1 /\ dsnd (x2 <: (dtuple2 (t_of v) (vdep_payload v p))) == snd x1 let intro_vdep_lemma (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (m: mem) : Lemma (requires ( interp (hp_of (v `star` q)) m /\ q == p (fst (sel_of (v `star` q) m)) )) (ensures ( interp (hp_of (v `star` q)) m /\ interp (hp_of (vdep v p)) m /\ vdep_rel v q p (sel_of (v `star` q) m) (sel_of (vdep v p) m) )) = Mem.interp_star (hp_of v) (hp_of q) m; interp_vdep_hp v p m; vdep_sel_eq v p m let intro_vdep v q p = reveal_star v q; change_slprop_rel_with_cond (v `star` q) (vdep v p) (vdep_cond v q p) (vdep_rel v q p) (fun m -> intro_vdep_lemma v q p m) let vdep_cond_recip (v: vprop) (p: (t_of v -> Tot vprop)) (q: vprop) (x2: t_of (vdep v p)) : Tot prop = q == p (dfst (x2 <: dtuple2 (t_of v) (vdep_payload v p))) let vdep_rel_recip (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (x2: (t_of (vdep v p))) (x1: t_of (v `star` q)) : Tot prop = vdep_rel v q p x1 x2 let elim_vdep_lemma (v: vprop) (q: vprop) (p: (t_of v -> Tot vprop)) (m: mem) : Lemma (requires ( interp (hp_of (vdep v p)) m /\ q == p (dfst (sel_of (vdep v p) m <: dtuple2 (t_of v) (vdep_payload v p))) )) (ensures ( interp (hp_of (v `star` q)) m /\ interp (hp_of (vdep v p)) m /\ vdep_rel v q p (sel_of (v `star` q) m) (sel_of (vdep v p) m) )) = Mem.interp_star (hp_of v) (hp_of q) m; interp_vdep_hp v p m; vdep_sel_eq v p m
{ "checked_file": "/", "dependencies": [ "Steel.Semantics.Hoare.MST.fst.checked", "Steel.Memory.fsti.checked", "Steel.Effect.fst.checked", "Steel.Effect.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.NMSTTotal.fst.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "Steel.Effect.Atomic.fst" }
[ { "abbrev": true, "full_module": "FStar.Universe", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "Steel.Semantics.Hoare.MST", "short_module": "Sem" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Common", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics", "short_module": "T" }, { "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": 1, "initial_ifuel": 1, "max_fuel": 1, "max_ifuel": 1, "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": 20, "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) -> q: Steel.Effect.Common.vprop -> Steel.Effect.Atomic.SteelGhost Prims.unit
Steel.Effect.Atomic.SteelGhost
[]
[]
[ "Steel.Memory.inames", "Steel.Effect.Common.vprop", "Steel.Effect.Common.t_of", "Steel.Effect.Atomic.reveal_star", "Prims.unit", "Steel.Effect.Atomic.change_slprop_rel_with_cond", "Steel.Effect.Common.vdep", "Steel.Effect.Common.star", "Steel.Effect.Atomic.vdep_cond_recip", "Steel.Effect.Atomic.vdep_rel_recip", "Steel.Memory.mem", "Steel.Effect.Atomic.elim_vdep_lemma", "Steel.Effect.Common.rmem", "Prims.eq2", "FStar.Pervasives.dfst", "Steel.Effect.Common.vdep_payload", "Prims.l_and", "Steel.Effect.Common.normal", "FStar.Pervasives.dsnd", "FStar.Pervasives.norm", "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 elim_vdep0 (#opened: inames) (v: vprop) (p: (t_of v -> Tot vprop)) (q: vprop) : SteelGhost unit opened (vdep v p) (fun _ -> v `star` q) (requires (fun h -> q == p (dfst (h (vdep v p))))) (ensures (fun h _ h' -> let fs = h' v in let sn = h' q in let x2 = h (vdep v p) in q == p fs /\ dfst x2 == fs /\ dsnd x2 == sn)) =
change_slprop_rel_with_cond (vdep v p) (v `star` q) (vdep_cond_recip v p q) (vdep_rel_recip v q p) (fun m -> elim_vdep_lemma v q p m); reveal_star v q
false
FStar.Pointer.Derived2.fst
FStar.Pointer.Derived2.copy_buffer_contents_inv
val copy_buffer_contents_inv (#t: typ) (a b: buffer t) (len': UInt32.t) (h h': HS.mem) : GTot Type0
val copy_buffer_contents_inv (#t: typ) (a b: buffer t) (len': UInt32.t) (h h': HS.mem) : GTot Type0
let copy_buffer_contents_inv (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (len' : UInt32.t) (h: HS.mem) (h' : HS.mem) : GTot Type0 = copy_buffer_contents_precond' a b h /\ modifies (loc_buffer b) h h' /\ UInt32.v len' <= UInt32.v (buffer_length a) /\ buffer_readable h' (gsub_buffer b 0ul len') /\ buffer_as_seq h' (gsub_buffer b 0ul len') == buffer_as_seq h (gsub_buffer a 0ul len')
{ "file_name": "ulib/legacy/FStar.Pointer.Derived2.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 87, "end_line": 60, "start_col": 0, "start_line": 48 }
(* Copyright 2008-2018 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 FStar.Pointer.Derived2 module HH = FStar.HyperStack module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let copy_buffer_contents_precond' (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) : GTot Type0 = buffer_live h b /\ buffer_readable h a /\ buffer_length b == buffer_length a /\ loc_disjoint (loc_buffer a) (loc_buffer b) private let copy_buffer_contents_postcond' (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) (h' : HS.mem) : GTot Type0 = copy_buffer_contents_precond' a b h /\ modifies (loc_buffer b) h h' /\ buffer_readable h' b /\ buffer_as_seq h' b == buffer_as_seq h a
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": true, "source_file": "FStar.Pointer.Derived2.fst" }
[ { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HH" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HH" }, { "abbrev": false, "full_module": "FStar.Pointer.Derived1", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer", "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: FStar.Pointer.Base.buffer t -> b: FStar.Pointer.Base.buffer t -> len': FStar.UInt32.t -> h: FStar.Monotonic.HyperStack.mem -> h': FStar.Monotonic.HyperStack.mem -> Prims.GTot Type0
Prims.GTot
[ "sometrivial" ]
[]
[ "FStar.Pointer.Base.typ", "FStar.Pointer.Base.buffer", "FStar.UInt32.t", "FStar.Monotonic.HyperStack.mem", "Prims.l_and", "FStar.Pointer.Derived2.copy_buffer_contents_precond'", "FStar.Pointer.Base.modifies", "FStar.Pointer.Base.loc_buffer", "Prims.b2t", "Prims.op_LessThanOrEqual", "FStar.UInt32.v", "FStar.Pointer.Base.buffer_length", "FStar.Pointer.Base.buffer_readable", "FStar.Pointer.Base.gsub_buffer", "FStar.UInt32.__uint_to_t", "Prims.eq2", "FStar.Seq.Base.seq", "FStar.Pointer.Base.type_of_typ", "FStar.Pointer.Base.buffer_as_seq" ]
[]
false
false
false
false
true
let copy_buffer_contents_inv (#t: typ) (a b: buffer t) (len': UInt32.t) (h h': HS.mem) : GTot Type0 =
copy_buffer_contents_precond' a b h /\ modifies (loc_buffer b) h h' /\ UInt32.v len' <= UInt32.v (buffer_length a) /\ buffer_readable h' (gsub_buffer b 0ul len') /\ buffer_as_seq h' (gsub_buffer b 0ul len') == buffer_as_seq h (gsub_buffer a 0ul len')
false
FStar.Pointer.Derived2.fst
FStar.Pointer.Derived2.copy_buffer_contents_init
val copy_buffer_contents_init (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) : Lemma (requires (copy_buffer_contents_precond' a b h)) (ensures (copy_buffer_contents_inv a b 0ul h h))
val copy_buffer_contents_init (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) : Lemma (requires (copy_buffer_contents_precond' a b h)) (ensures (copy_buffer_contents_inv a b 0ul h h))
let copy_buffer_contents_init #t a b h = buffer_readable_intro_empty h (gsub_buffer b 0ul 0ul); Seq.lemma_eq_intro (buffer_as_seq h (gsub_buffer b 0ul 0ul)) (buffer_as_seq h (gsub_buffer a 0ul 0ul))
{ "file_name": "ulib/legacy/FStar.Pointer.Derived2.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 104, "end_line": 74, "start_col": 0, "start_line": 72 }
(* Copyright 2008-2018 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 FStar.Pointer.Derived2 module HH = FStar.HyperStack module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let copy_buffer_contents_precond' (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) : GTot Type0 = buffer_live h b /\ buffer_readable h a /\ buffer_length b == buffer_length a /\ loc_disjoint (loc_buffer a) (loc_buffer b) private let copy_buffer_contents_postcond' (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) (h' : HS.mem) : GTot Type0 = copy_buffer_contents_precond' a b h /\ modifies (loc_buffer b) h h' /\ buffer_readable h' b /\ buffer_as_seq h' b == buffer_as_seq h a private let copy_buffer_contents_inv (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (len' : UInt32.t) (h: HS.mem) (h' : HS.mem) : GTot Type0 = copy_buffer_contents_precond' a b h /\ modifies (loc_buffer b) h h' /\ UInt32.v len' <= UInt32.v (buffer_length a) /\ buffer_readable h' (gsub_buffer b 0ul len') /\ buffer_as_seq h' (gsub_buffer b 0ul len') == buffer_as_seq h (gsub_buffer a 0ul len') private val copy_buffer_contents_init (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) : Lemma (requires (copy_buffer_contents_precond' a b h)) (ensures (copy_buffer_contents_inv a b 0ul h h))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": true, "source_file": "FStar.Pointer.Derived2.fst" }
[ { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HH" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HH" }, { "abbrev": false, "full_module": "FStar.Pointer.Derived1", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer", "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: FStar.Pointer.Base.buffer t -> b: FStar.Pointer.Base.buffer t -> h: FStar.Monotonic.HyperStack.mem -> FStar.Pervasives.Lemma (requires FStar.Pointer.Derived2.copy_buffer_contents_precond' a b h) (ensures FStar.Pointer.Derived2.copy_buffer_contents_inv a b 0ul h h)
FStar.Pervasives.Lemma
[ "lemma" ]
[]
[ "FStar.Pointer.Base.typ", "FStar.Pointer.Base.buffer", "FStar.Monotonic.HyperStack.mem", "FStar.Seq.Base.lemma_eq_intro", "FStar.Pointer.Base.type_of_typ", "FStar.Pointer.Base.buffer_as_seq", "FStar.Pointer.Base.gsub_buffer", "FStar.UInt32.__uint_to_t", "Prims.unit", "FStar.Pointer.Derived1.buffer_readable_intro_empty" ]
[]
true
false
true
false
false
let copy_buffer_contents_init #t a b h =
buffer_readable_intro_empty h (gsub_buffer b 0ul 0ul); Seq.lemma_eq_intro (buffer_as_seq h (gsub_buffer b 0ul 0ul)) (buffer_as_seq h (gsub_buffer a 0ul 0ul))
false
FStar.Pointer.Derived2.fst
FStar.Pointer.Derived2.copy_buffer_contents'
val copy_buffer_contents' (#t: typ) (a: buffer t) (idx_a: UInt32.t) (b: buffer t) (idx_b len: UInt32.t) : HST.Stack unit (requires (fun h -> copy_buffer_contents_precond a idx_a b idx_b len h)) (ensures (fun h0 _ h1 -> copy_buffer_contents_postcond a idx_a b idx_b len h0 h1))
val copy_buffer_contents' (#t: typ) (a: buffer t) (idx_a: UInt32.t) (b: buffer t) (idx_b len: UInt32.t) : HST.Stack unit (requires (fun h -> copy_buffer_contents_precond a idx_a b idx_b len h)) (ensures (fun h0 _ h1 -> copy_buffer_contents_postcond a idx_a b idx_b len h0 h1))
let copy_buffer_contents' (#t: typ) (a: buffer t) (* source *) (idx_a: UInt32.t) (b: buffer t) (* destination *) (idx_b: UInt32.t) (len: UInt32.t) : HST.Stack unit (requires (fun h -> copy_buffer_contents_precond a idx_a b idx_b len h )) (ensures (fun h0 _ h1 -> copy_buffer_contents_postcond a idx_a b idx_b len h0 h1 )) = let h0 = HST.get () in let a' = sub_buffer a idx_a len in let b' = sub_buffer b idx_b len in copy_buffer_contents_init a' b' h0; copy_buffer_contents_aux a' b' len 0ul (Ghost.hide h0); let h1 = HST.get () in copy_buffer_contents_fin a idx_a b idx_b len h0 h1
{ "file_name": "ulib/legacy/FStar.Pointer.Derived2.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 52, "end_line": 172, "start_col": 0, "start_line": 152 }
(* Copyright 2008-2018 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 FStar.Pointer.Derived2 module HH = FStar.HyperStack module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let copy_buffer_contents_precond' (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) : GTot Type0 = buffer_live h b /\ buffer_readable h a /\ buffer_length b == buffer_length a /\ loc_disjoint (loc_buffer a) (loc_buffer b) private let copy_buffer_contents_postcond' (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) (h' : HS.mem) : GTot Type0 = copy_buffer_contents_precond' a b h /\ modifies (loc_buffer b) h h' /\ buffer_readable h' b /\ buffer_as_seq h' b == buffer_as_seq h a private let copy_buffer_contents_inv (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (len' : UInt32.t) (h: HS.mem) (h' : HS.mem) : GTot Type0 = copy_buffer_contents_precond' a b h /\ modifies (loc_buffer b) h h' /\ UInt32.v len' <= UInt32.v (buffer_length a) /\ buffer_readable h' (gsub_buffer b 0ul len') /\ buffer_as_seq h' (gsub_buffer b 0ul len') == buffer_as_seq h (gsub_buffer a 0ul len') private val copy_buffer_contents_init (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) : Lemma (requires (copy_buffer_contents_precond' a b h)) (ensures (copy_buffer_contents_inv a b 0ul h h)) let copy_buffer_contents_init #t a b h = buffer_readable_intro_empty h (gsub_buffer b 0ul 0ul); Seq.lemma_eq_intro (buffer_as_seq h (gsub_buffer b 0ul 0ul)) (buffer_as_seq h (gsub_buffer a 0ul 0ul)) private val copy_buffer_contents_advance (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (len' : UInt32.t) (h: Ghost.erased HS.mem) : HST.Stack unit (requires (fun h0 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h0 /\ UInt32.v len' < UInt32.v (buffer_length a) )) (ensures (fun h1 _ h2 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h1 /\ UInt32.v len' < UInt32.v (buffer_length a) /\ copy_buffer_contents_inv a b (UInt32.add len' 1ul) (Ghost.reveal h) h2 )) #set-options "--z3rlimit 16" let copy_buffer_contents_advance #t a b len' h = let v = read_buffer a len' in buffer_snoc b 0ul len' v; buffer_as_seq_gsub_buffer_snoc (Ghost.reveal h) a 0ul len' private val copy_buffer_contents_aux (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (len: UInt32.t) (len': UInt32.t) (h: Ghost.erased HS.mem) : HST.Stack unit (requires (fun h0 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h0 /\ len == buffer_length a )) (ensures (fun h0 _ h1 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h0 /\ copy_buffer_contents_postcond' a b (Ghost.reveal h) h1 )) (decreases (UInt32.v (buffer_length a) - UInt32.v len')) let rec copy_buffer_contents_aux #t a b len len' h = if len = len' then () else begin copy_buffer_contents_advance a b len' h; copy_buffer_contents_aux a b len (UInt32.add len' 1ul) h end let copy_buffer_contents_fin (#t: typ) (a: buffer t) (* source *) (idx_a: UInt32.t) (b: buffer t) (* destination *) (idx_b: UInt32.t) (len: UInt32.t) (h: HS.mem) (h' : HS.mem) : Lemma (requires ( copy_buffer_contents_precond a idx_a b idx_b len h /\ copy_buffer_contents_postcond' (gsub_buffer a idx_a len) (gsub_buffer b idx_b len) h h' )) (ensures ( copy_buffer_contents_precond a idx_a b idx_b len h /\ copy_buffer_contents_postcond a idx_a b idx_b len h h' )) = () (* FIXME: Does not work if I directly try to define copy_buffer_contents *) (* FIXME: Works in batch mode (even with --record_hints --use_hints --detail_hint_replay --query_stats) but fails in interactive mode *)
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": true, "source_file": "FStar.Pointer.Derived2.fst" }
[ { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HH" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HH" }, { "abbrev": false, "full_module": "FStar.Pointer.Derived1", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer", "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": 16, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.Pointer.Base.buffer t -> idx_a: FStar.UInt32.t -> b: FStar.Pointer.Base.buffer t -> idx_b: FStar.UInt32.t -> len: FStar.UInt32.t -> FStar.HyperStack.ST.Stack Prims.unit
FStar.HyperStack.ST.Stack
[]
[]
[ "FStar.Pointer.Base.typ", "FStar.Pointer.Base.buffer", "FStar.UInt32.t", "FStar.Pointer.Derived2.copy_buffer_contents_fin", "Prims.unit", "FStar.Monotonic.HyperStack.mem", "FStar.HyperStack.ST.get", "FStar.Pointer.Derived2.copy_buffer_contents_aux", "FStar.UInt32.__uint_to_t", "FStar.Ghost.hide", "FStar.Pointer.Derived2.copy_buffer_contents_init", "FStar.Pointer.Base.sub_buffer", "FStar.Pointer.Derived2.copy_buffer_contents_precond", "FStar.Pointer.Derived2.copy_buffer_contents_postcond" ]
[]
false
true
false
false
false
let copy_buffer_contents' (#t: typ) (a: buffer t) (idx_a: UInt32.t) (b: buffer t) (idx_b len: UInt32.t) : HST.Stack unit (requires (fun h -> copy_buffer_contents_precond a idx_a b idx_b len h)) (ensures (fun h0 _ h1 -> copy_buffer_contents_postcond a idx_a b idx_b len h0 h1)) =
let h0 = HST.get () in let a' = sub_buffer a idx_a len in let b' = sub_buffer b idx_b len in copy_buffer_contents_init a' b' h0; copy_buffer_contents_aux a' b' len 0ul (Ghost.hide h0); let h1 = HST.get () in copy_buffer_contents_fin a idx_a b idx_b len h0 h1
false
FStar.Pointer.Derived2.fst
FStar.Pointer.Derived2.copy_buffer_contents_aux
val copy_buffer_contents_aux (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (len: UInt32.t) (len': UInt32.t) (h: Ghost.erased HS.mem) : HST.Stack unit (requires (fun h0 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h0 /\ len == buffer_length a )) (ensures (fun h0 _ h1 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h0 /\ copy_buffer_contents_postcond' a b (Ghost.reveal h) h1 )) (decreases (UInt32.v (buffer_length a) - UInt32.v len'))
val copy_buffer_contents_aux (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (len: UInt32.t) (len': UInt32.t) (h: Ghost.erased HS.mem) : HST.Stack unit (requires (fun h0 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h0 /\ len == buffer_length a )) (ensures (fun h0 _ h1 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h0 /\ copy_buffer_contents_postcond' a b (Ghost.reveal h) h1 )) (decreases (UInt32.v (buffer_length a) - UInt32.v len'))
let rec copy_buffer_contents_aux #t a b len len' h = if len = len' then () else begin copy_buffer_contents_advance a b len' h; copy_buffer_contents_aux a b len (UInt32.add len' 1ul) h end
{ "file_name": "ulib/legacy/FStar.Pointer.Derived2.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 5, "end_line": 126, "start_col": 0, "start_line": 120 }
(* Copyright 2008-2018 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 FStar.Pointer.Derived2 module HH = FStar.HyperStack module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let copy_buffer_contents_precond' (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) : GTot Type0 = buffer_live h b /\ buffer_readable h a /\ buffer_length b == buffer_length a /\ loc_disjoint (loc_buffer a) (loc_buffer b) private let copy_buffer_contents_postcond' (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) (h' : HS.mem) : GTot Type0 = copy_buffer_contents_precond' a b h /\ modifies (loc_buffer b) h h' /\ buffer_readable h' b /\ buffer_as_seq h' b == buffer_as_seq h a private let copy_buffer_contents_inv (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (len' : UInt32.t) (h: HS.mem) (h' : HS.mem) : GTot Type0 = copy_buffer_contents_precond' a b h /\ modifies (loc_buffer b) h h' /\ UInt32.v len' <= UInt32.v (buffer_length a) /\ buffer_readable h' (gsub_buffer b 0ul len') /\ buffer_as_seq h' (gsub_buffer b 0ul len') == buffer_as_seq h (gsub_buffer a 0ul len') private val copy_buffer_contents_init (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) : Lemma (requires (copy_buffer_contents_precond' a b h)) (ensures (copy_buffer_contents_inv a b 0ul h h)) let copy_buffer_contents_init #t a b h = buffer_readable_intro_empty h (gsub_buffer b 0ul 0ul); Seq.lemma_eq_intro (buffer_as_seq h (gsub_buffer b 0ul 0ul)) (buffer_as_seq h (gsub_buffer a 0ul 0ul)) private val copy_buffer_contents_advance (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (len' : UInt32.t) (h: Ghost.erased HS.mem) : HST.Stack unit (requires (fun h0 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h0 /\ UInt32.v len' < UInt32.v (buffer_length a) )) (ensures (fun h1 _ h2 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h1 /\ UInt32.v len' < UInt32.v (buffer_length a) /\ copy_buffer_contents_inv a b (UInt32.add len' 1ul) (Ghost.reveal h) h2 )) #set-options "--z3rlimit 16" let copy_buffer_contents_advance #t a b len' h = let v = read_buffer a len' in buffer_snoc b 0ul len' v; buffer_as_seq_gsub_buffer_snoc (Ghost.reveal h) a 0ul len' private val copy_buffer_contents_aux (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (len: UInt32.t) (len': UInt32.t) (h: Ghost.erased HS.mem) : HST.Stack unit (requires (fun h0 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h0 /\ len == buffer_length a )) (ensures (fun h0 _ h1 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h0 /\ copy_buffer_contents_postcond' a b (Ghost.reveal h) h1 )) (decreases (UInt32.v (buffer_length a) - UInt32.v len'))
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": true, "source_file": "FStar.Pointer.Derived2.fst" }
[ { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HH" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HH" }, { "abbrev": false, "full_module": "FStar.Pointer.Derived1", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer", "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": 16, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.Pointer.Base.buffer t -> b: FStar.Pointer.Base.buffer t -> len: FStar.UInt32.t -> len': FStar.UInt32.t -> h: FStar.Ghost.erased FStar.Monotonic.HyperStack.mem -> FStar.HyperStack.ST.Stack Prims.unit
FStar.HyperStack.ST.Stack
[ "" ]
[]
[ "FStar.Pointer.Base.typ", "FStar.Pointer.Base.buffer", "FStar.UInt32.t", "FStar.Ghost.erased", "FStar.Monotonic.HyperStack.mem", "Prims.op_Equality", "Prims.unit", "Prims.bool", "FStar.Pointer.Derived2.copy_buffer_contents_aux", "FStar.UInt32.add", "FStar.UInt32.__uint_to_t", "FStar.Pointer.Derived2.copy_buffer_contents_advance" ]
[ "recursion" ]
false
true
false
false
false
let rec copy_buffer_contents_aux #t a b len len' h =
if len = len' then () else (copy_buffer_contents_advance a b len' h; copy_buffer_contents_aux a b len (UInt32.add len' 1ul) h)
false
FStar.Pointer.Derived2.fst
FStar.Pointer.Derived2.copy_buffer_contents_advance
val copy_buffer_contents_advance (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (len' : UInt32.t) (h: Ghost.erased HS.mem) : HST.Stack unit (requires (fun h0 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h0 /\ UInt32.v len' < UInt32.v (buffer_length a) )) (ensures (fun h1 _ h2 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h1 /\ UInt32.v len' < UInt32.v (buffer_length a) /\ copy_buffer_contents_inv a b (UInt32.add len' 1ul) (Ghost.reveal h) h2 ))
val copy_buffer_contents_advance (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (len' : UInt32.t) (h: Ghost.erased HS.mem) : HST.Stack unit (requires (fun h0 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h0 /\ UInt32.v len' < UInt32.v (buffer_length a) )) (ensures (fun h1 _ h2 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h1 /\ UInt32.v len' < UInt32.v (buffer_length a) /\ copy_buffer_contents_inv a b (UInt32.add len' 1ul) (Ghost.reveal h) h2 ))
let copy_buffer_contents_advance #t a b len' h = let v = read_buffer a len' in buffer_snoc b 0ul len' v; buffer_as_seq_gsub_buffer_snoc (Ghost.reveal h) a 0ul len'
{ "file_name": "ulib/legacy/FStar.Pointer.Derived2.fst", "git_rev": "10183ea187da8e8c426b799df6c825e24c0767d3", "git_url": "https://github.com/FStarLang/FStar.git", "project_name": "FStar" }
{ "end_col": 60, "end_line": 99, "start_col": 0, "start_line": 96 }
(* Copyright 2008-2018 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 FStar.Pointer.Derived2 module HH = FStar.HyperStack module HS = FStar.HyperStack module HST = FStar.HyperStack.ST private let copy_buffer_contents_precond' (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) : GTot Type0 = buffer_live h b /\ buffer_readable h a /\ buffer_length b == buffer_length a /\ loc_disjoint (loc_buffer a) (loc_buffer b) private let copy_buffer_contents_postcond' (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) (h' : HS.mem) : GTot Type0 = copy_buffer_contents_precond' a b h /\ modifies (loc_buffer b) h h' /\ buffer_readable h' b /\ buffer_as_seq h' b == buffer_as_seq h a private let copy_buffer_contents_inv (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (len' : UInt32.t) (h: HS.mem) (h' : HS.mem) : GTot Type0 = copy_buffer_contents_precond' a b h /\ modifies (loc_buffer b) h h' /\ UInt32.v len' <= UInt32.v (buffer_length a) /\ buffer_readable h' (gsub_buffer b 0ul len') /\ buffer_as_seq h' (gsub_buffer b 0ul len') == buffer_as_seq h (gsub_buffer a 0ul len') private val copy_buffer_contents_init (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (h: HS.mem) : Lemma (requires (copy_buffer_contents_precond' a b h)) (ensures (copy_buffer_contents_inv a b 0ul h h)) let copy_buffer_contents_init #t a b h = buffer_readable_intro_empty h (gsub_buffer b 0ul 0ul); Seq.lemma_eq_intro (buffer_as_seq h (gsub_buffer b 0ul 0ul)) (buffer_as_seq h (gsub_buffer a 0ul 0ul)) private val copy_buffer_contents_advance (#t: typ) (a: buffer t) (* source *) (b: buffer t) (* destination *) (len' : UInt32.t) (h: Ghost.erased HS.mem) : HST.Stack unit (requires (fun h0 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h0 /\ UInt32.v len' < UInt32.v (buffer_length a) )) (ensures (fun h1 _ h2 -> copy_buffer_contents_inv a b len' (Ghost.reveal h) h1 /\ UInt32.v len' < UInt32.v (buffer_length a) /\ copy_buffer_contents_inv a b (UInt32.add len' 1ul) (Ghost.reveal h) h2 )) #set-options "--z3rlimit 16"
{ "checked_file": "/", "dependencies": [ "prims.fst.checked", "FStar.UInt32.fsti.checked", "FStar.Seq.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.HyperStack.ST.fsti.checked", "FStar.HyperStack.fst.checked", "FStar.Ghost.fsti.checked" ], "interface_file": true, "source_file": "FStar.Pointer.Derived2.fst" }
[ { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HH" }, { "abbrev": true, "full_module": "FStar.HyperStack.ST", "short_module": "HST" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HS" }, { "abbrev": true, "full_module": "FStar.HyperStack", "short_module": "HH" }, { "abbrev": false, "full_module": "FStar.Pointer.Derived1", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer", "short_module": null }, { "abbrev": false, "full_module": "FStar.Pointer", "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": 16, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: FStar.Pointer.Base.buffer t -> b: FStar.Pointer.Base.buffer t -> len': FStar.UInt32.t -> h: FStar.Ghost.erased FStar.Monotonic.HyperStack.mem -> FStar.HyperStack.ST.Stack Prims.unit
FStar.HyperStack.ST.Stack
[]
[]
[ "FStar.Pointer.Base.typ", "FStar.Pointer.Base.buffer", "FStar.UInt32.t", "FStar.Ghost.erased", "FStar.Monotonic.HyperStack.mem", "FStar.Pointer.Derived1.buffer_as_seq_gsub_buffer_snoc", "FStar.Ghost.reveal", "FStar.UInt32.__uint_to_t", "Prims.unit", "FStar.Pointer.Derived1.buffer_snoc", "FStar.Pointer.Base.type_of_typ", "FStar.Pointer.Base.read_buffer" ]
[]
false
true
false
false
false
let copy_buffer_contents_advance #t a b len' h =
let v = read_buffer a len' in buffer_snoc b 0ul len' v; buffer_as_seq_gsub_buffer_snoc (Ghost.reveal h) a 0ul len'
false
Steel.Effect.Atomic.fst
Steel.Effect.Atomic.intro_vrefine
val intro_vrefine (#opened:inames) (v: vprop) (p: (normal (t_of v) -> Tot prop)) : SteelGhost unit opened v (fun _ -> vrefine v p) (requires fun h -> p (h v)) (ensures fun h _ h' -> h' (vrefine v p) == h v)
val intro_vrefine (#opened:inames) (v: vprop) (p: (normal (t_of v) -> Tot prop)) : SteelGhost unit opened v (fun _ -> vrefine v p) (requires fun h -> p (h v)) (ensures fun h _ h' -> h' (vrefine v p) == h v)
let intro_vrefine v p = let m = get () in let x : Ghost.erased (t_of v) = gget v in let x' : Ghost.erased (vrefine_t v p) = Ghost.hide (Ghost.reveal x) in change_slprop v (vrefine v p) x x' (fun m -> interp_vrefine_hp v p m; vrefine_sel_eq v p m )
{ "file_name": "lib/steel/Steel.Effect.Atomic.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 5, "end_line": 705, "start_col": 0, "start_line": 693 }
(* 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.Atomic open Steel.Effect friend Steel.Effect #set-options "--warn_error -330" //turn off the experimental feature warning let _ : squash (forall (pre:pre_t) (m0:mem{interp (hp_of pre) m0}) (m1:mem{disjoint m0 m1}). mk_rmem pre m0 == mk_rmem pre (join m0 m1)) = Classical.forall_intro rmem_depends_only_on let req_to_act_req (#pre:vprop) (req:req_t pre) : mprop (hp_of pre) = fun m -> rmem_depends_only_on pre; interp (hp_of pre) m /\ req (mk_rmem pre m) unfold let to_post (#a:Type) (post:post_t a) = fun x -> (hp_of (post x)) let ens_to_act_ens (#pre:pre_t) (#a:Type) (#post:post_t a) (ens:ens_t pre a post) : mprop2 (hp_of pre) (to_post post) = fun m0 x m1 -> interp (hp_of pre) m0 /\ interp (hp_of (post x)) m1 /\ ens (mk_rmem pre m0) x (mk_rmem (post x) m1) let repr a framed opened f pre post req ens = action_except_full a opened (hp_of pre) (to_post post) (req_to_act_req req) (ens_to_act_ens ens) let return_ a x opened #p = fun _ -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (p x) (core_mem m0) in lemma_frame_equalities_refl (p x) h0; x #push-options "--fuel 0 --ifuel 0" #push-options "--z3rlimit 20 --fuel 1 --ifuel 1" val frame00 (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:repr a framed opened obs pre post req ens) (frame:vprop) : repr a true opened obs (pre `star` frame) (fun x -> post x `star` frame) (fun h -> req (focus_rmem h pre)) (fun h0 r h1 -> req (focus_rmem h0 pre) /\ ens (focus_rmem h0 pre) r (focus_rmem h1 (post r)) /\ frame_opaque frame (focus_rmem h0 frame) (focus_rmem h1 frame)) module Sem = Steel.Semantics.Hoare.MST module Mem = Steel.Memory let equiv_middle_left_assoc (a b c d:slprop) : Lemma (((a `Mem.star` b) `Mem.star` c `Mem.star` d) `Mem.equiv` (a `Mem.star` (b `Mem.star` c) `Mem.star` d)) = let open Steel.Memory in star_associative a b c; star_congruence ((a `star` b) `star` c) d (a `star` (b `star` c)) d let frame00 #a #framed #opened #obs #pre #post #req #ens f frame = fun frame' -> let m0:full_mem = NMSTTotal.get () in let snap:rmem frame = mk_rmem frame (core_mem m0) in // Need to define it with type annotation, although unused, for it to trigger // the pattern on the framed ensures in the def of MstTot let aux:mprop (hp_of frame `Mem.star` frame') = req_frame frame snap in focus_is_restrict_mk_rmem (pre `star` frame) pre (core_mem m0); assert (interp (hp_of (pre `star` frame) `Mem.star` frame' `Mem.star` locks_invariant opened m0) m0); equiv_middle_left_assoc (hp_of pre) (hp_of frame) frame' (locks_invariant opened m0); assert (interp (hp_of pre `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m0) m0); let x = f (hp_of frame `Mem.star` frame') in let m1:full_mem = NMSTTotal.get () in assert (interp (hp_of (post x) `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m1) m1); equiv_middle_left_assoc (hp_of (post x)) (hp_of frame) frame' (locks_invariant opened m1); assert (interp ((hp_of (post x) `Mem.star` hp_of frame) `Mem.star` frame' `Mem.star` locks_invariant opened m1) m1); focus_is_restrict_mk_rmem (pre `star` frame) frame (core_mem m0); focus_is_restrict_mk_rmem (post x `star` frame) frame (core_mem m1); let h0:rmem (pre `star` frame) = mk_rmem (pre `star` frame) (core_mem m0) in let h1:rmem (post x `star` frame) = mk_rmem (post x `star` frame) (core_mem m1) in assert (focus_rmem h0 frame == focus_rmem h1 frame); focus_is_restrict_mk_rmem (post x `star` frame) (post x) (core_mem m1); lemma_frame_opaque_refl frame (focus_rmem h0 frame); x unfold let bind_req_opaque (#a:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#pr:a -> prop) (req_g:(x:a -> req_t (pre_g x))) (frame_f:vprop) (frame_g:a -> vprop) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) : req_t (pre_f `star` frame_f) = fun m0 -> req_f (focus_rmem m0 pre_f) /\ (forall (x:a) (h1:hmem (post_f x `star` frame_f)). (ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f)) ==> pr x /\ (can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); (req_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)))) unfold let bind_ens_opaque (#a:Type) (#b:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#pr:a -> prop) (ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (frame_f:vprop) (frame_g:a -> vprop) (post:post_t b) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (_:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) : ens_t (pre_f `star` frame_f) b post = fun m0 y m2 -> req_f (focus_rmem m0 pre_f) /\ (exists (x:a) (h1:hmem (post_f x `star` frame_f)). pr x /\ ( can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f) /\ frame_opaque (frame_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (frame_g x)) (focus_rmem m2 (frame_g x)) /\ ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ (ens_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)) y (focus_rmem m2 (post_g x y)))) val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True) #push-options "--z3rlimit 20" let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_f x `star` frame_f) (core_mem m1) in let h1' = mk_rmem (pre_g x `star` frame_g x) (core_mem m1) in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); focus_is_restrict_mk_rmem (post_f x `star` frame_f) (pre_g x `star` frame_g x) (core_mem m1); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of (post_f x `star` frame_f)) (hp_of (pre_g x `star` frame_g x)) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); let h2' = mk_rmem (post_g x y `star` frame_g x) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in // assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem (post_g x y `star` frame_g x) (post y) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of (post_g x y `star` frame_g x)) (hp_of (post y)) frame (locks_invariant opened m2) m2; y let norm_repr (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) (f:repr a framed opened obs pre post req ens) : repr a framed opened obs pre post (fun h -> norm_opaque (req h)) (fun h0 x h1 -> norm_opaque (ens h0 x h1)) = f let bind a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = norm_repr (bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g) val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True) let subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem pre_g (core_mem m0) in can_be_split_trans pre_g (pre_f `star` fr) pre_f; can_be_split_trans pre_g (pre_f `star` fr) fr; can_be_split_3_interp (hp_of pre_g) (hp_of (pre_f `star` fr)) frame (locks_invariant opened m0) m0; focus_replace pre_g (pre_f `star` fr) pre_f (core_mem m0); let x = frame00 f fr frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_g x) (core_mem m1) in let h0' = mk_rmem (pre_f `star` fr) (core_mem m0) in let h1' = mk_rmem (post_f x `star` fr) (core_mem m1) in // From frame00 assert (frame_opaque fr (focus_rmem h0' fr) (focus_rmem h1' fr)); // Replace h0'/h1' by h0/h1 focus_replace pre_g (pre_f `star` fr) fr (core_mem m0); focus_replace (post_g x) (post_f x `star` fr) fr (core_mem m1); assert (frame_opaque fr (focus_rmem h0 fr) (focus_rmem h1 fr)); can_be_split_trans (post_g x) (post_f x `star` fr) (post_f x); can_be_split_trans (post_g x) (post_f x `star` fr) fr; can_be_split_3_interp (hp_of (post_f x `star` fr)) (hp_of (post_g x)) frame (locks_invariant opened m1) m1; focus_replace (post_g x) (post_f x `star` fr) (post_f x) (core_mem m1); x let subcomp a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #_ #pr #p1 #p2 f = lemma_subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2; subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f #pop-options let bind_pure_steela_ a b opened o #wp f g = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun frame -> let x = f () in g x frame let lift_ghost_atomic a o f = f let lift_atomic_steel a o f = f let as_atomic_action f = SteelAtomic?.reflect f let as_atomic_action_ghost f = SteelGhost?.reflect f let as_atomic_unobservable_action f = SteelAtomicU?.reflect f (* Some helpers *) let get0 (#opened:inames) (#p:vprop) (_:unit) : repr (erased (rmem p)) true opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1 /\ frame_equalities p r h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; h0 let get () = SteelGhost?.reflect (get0 ()) let intro_star (p q:vprop) (r:slprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (m:mem) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m) ) : Lemma (requires interp ((hp_of p) `Mem.star` r) m /\ sel_of p m == reveal vp) (ensures interp ((hp_of q) `Mem.star` r) m) = let p = hp_of p in let q = hp_of q in let intro (ml mr:mem) : Lemma (requires interp q ml /\ interp r mr /\ disjoint ml mr) (ensures disjoint ml mr /\ interp (q `Mem.star` r) (Mem.join ml mr)) = Mem.intro_star q r ml mr in elim_star p r m; Classical.forall_intro (Classical.move_requires proof); Classical.forall_intro_2 (Classical.move_requires_2 intro) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let rewrite_slprop0 (#opened:inames) (p q:vprop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ _ -> True) = fun frame -> let m:full_mem = NMSTTotal.get () in proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) (sel_of q m) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let rewrite_slprop p q l = SteelGhost?.reflect (rewrite_slprop0 p q l) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop0 (#opened:inames) (p q:vprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun h -> h p == reveal vp) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) vp vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop p q vp vq l = SteelGhost?.reflect (change_slprop0 p q vp vq l) let change_equal_slprop p q = let m = get () in let x : Ghost.erased (t_of p) = hide ((reveal m) p) in let y : Ghost.erased (t_of q) = Ghost.hide (Ghost.reveal x) in change_slprop p q x y (fun _ -> ()) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop_20 (#opened:inames) (p q:vprop) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop_2 p q vq l = SteelGhost?.reflect (change_slprop_20 p q vq l) let change_slprop_rel0 (#opened:inames) (p q:vprop) (rel : normal (t_of p) -> normal (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel p q rel proof = SteelGhost?.reflect (change_slprop_rel0 p q rel proof) let change_slprop_rel_with_cond0 (#opened:inames) (p q:vprop) (cond: t_of p -> prop) (rel : (t_of p) -> (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ cond (sel_of p m)) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun m -> cond (m p)) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel_with_cond p q cond rel proof = SteelGhost?.reflect (change_slprop_rel_with_cond0 p q cond rel proof) let extract_info0 (#opened:inames) (p:vprop) (vp:erased (normal (t_of p))) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> h p == reveal vp) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info p vp fact l = SteelGhost?.reflect (extract_info0 p vp fact l) let extract_info_raw0 (#opened:inames) (p:vprop) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> True) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info_raw p fact l = SteelGhost?.reflect (extract_info_raw0 p fact l) let noop _ = change_slprop_rel emp emp (fun _ _ -> True) (fun _ -> ()) let sladmit _ = SteelGhostF?.reflect (fun _ -> NMSTTotal.nmst_tot_admit ()) let slassert0 (#opened:inames) (p:vprop) : repr unit false opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0 let slassert p = SteelGhost?.reflect (slassert0 p) let drop p = rewrite_slprop p emp (fun m -> emp_unit (hp_of p); affine_star (hp_of p) Mem.emp m; reveal_emp()) let reveal_star0 (#opened:inames) (p1 p2:vprop) : repr unit false opened Unobservable (p1 `star` p2) (fun _ -> p1 `star` p2) (fun _ -> True) (fun h0 _ h1 -> h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 (p1 `star` p2) == (h0 p1, h0 p2) /\ h1 (p1 `star` p2) == (h1 p1, h1 p2) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem let reveal_star p1 p2 = SteelGhost?.reflect (reveal_star0 p1 p2) let reveal_star_30 (#opened:inames) (p1 p2 p3:vprop) : repr unit false opened Unobservable (p1 `star` p2 `star` p3) (fun _ -> p1 `star` p2 `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split (p1 `star` p2 `star` p3) p1 /\ can_be_split (p1 `star` p2 `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 (p1 `star` p2 `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 (p1 `star` p2 `star` p3) == ((h1 p1, h1 p2), h1 p3) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem (p1 `star` p2 `star` p3) (core_mem m) in can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p1; can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p2; reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2 `star` p3); reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2); reveal_mk_rmem (p1 `star` p2 `star` p3) m p3 let reveal_star_3 p1 p2 p3 = SteelGhost?.reflect (reveal_star_30 p1 p2 p3) let intro_pure p = rewrite_slprop emp (pure p) (fun m -> pure_interp p m) let elim_pure_aux #uses (p:prop) : SteelGhostT (_:unit{p}) uses (pure p) (fun _ -> to_vprop Mem.emp) = as_atomic_action_ghost (Steel.Memory.elim_pure #uses p) let elim_pure #uses p = let _ = elim_pure_aux p in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()) let return #a #opened #p x = SteelAtomicBase?.reflect (return_ a x opened #p) let intro_exists #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists x (h_exists_sl' p) m) let intro_exists_erased #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists (Ghost.reveal x) (h_exists_sl' p) m) let witness_exists #a #u #p _ = SteelGhost?.reflect (Steel.Memory.witness_h_exists #u (fun x -> hp_of (p x))) let lift_exists #a #u p = as_atomic_action_ghost (Steel.Memory.lift_h_exists #u (fun x -> hp_of (p x))) let exists_equiv p q = Classical.forall_intro_2 reveal_equiv; h_exists_cong (h_exists_sl' p) (h_exists_sl' q) let exists_cong p q = rewrite_slprop (h_exists p) (h_exists q) (fun m -> reveal_equiv (h_exists p) (h_exists q); exists_equiv p q) let fresh_invariant #uses p ctxt = rewrite_slprop p (to_vprop (hp_of p)) (fun _ -> ()); let i = as_atomic_unobservable_action (fresh_invariant uses (hp_of p) ctxt) in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()); return i let new_invariant #uses p = let i = fresh_invariant #uses p [] in return i (* * AR: SteelAtomic and SteelGhost are not marked reifiable since we intend to run Steel programs natively * However to implement the with_inv combinators we need to reify their thunks to reprs * We could implement it better by having support for reification only in the .fst file * But for now assuming a function *) assume val reify_steel_atomic_comp (#a:Type) (#framed:bool) (#opened_invariants:inames) (#g:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:unit -> SteelAtomicBase a framed opened_invariants g pre post req ens) : repr a framed opened_invariants g pre post req ens [@@warn_on_use "as_unobservable_atomic_action is a trusted primitive"] let as_atomic_o_action (#a:Type u#a) (#opened_invariants:inames) (#fp:slprop) (#fp': a -> slprop) (o:observability) (f:action_except a opened_invariants fp fp') : SteelAtomicBaseT a opened_invariants o (to_vprop fp) (fun x -> to_vprop (fp' x)) = SteelAtomicBaseT?.reflect f let with_invariant #a #fp #fp' #obs #opened #p i f = rewrite_slprop fp (to_vprop (hp_of fp)) (fun _ -> ()); let x = as_atomic_o_action obs (Steel.Memory.with_invariant #a #(hp_of fp) #(fun x -> hp_of (fp' x)) #opened #(hp_of p) i (reify_steel_atomic_comp f)) in rewrite_slprop (to_vprop (hp_of (fp' x))) (fp' x) (fun _ -> ()); return x assume val reify_steel_ghost_comp (#a:Type) (#framed:bool) (#opened_invariants:inames) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:unit -> SteelGhostBase a framed opened_invariants Unobservable pre post req ens) : repr a framed opened_invariants Unobservable pre post req ens let with_invariant_g #a #fp #fp' #opened #p i f = rewrite_slprop fp (to_vprop (hp_of fp)) (fun _ -> ()); let x = as_atomic_unobservable_action (Steel.Memory.with_invariant #a #(hp_of fp) #(fun x -> hp_of (fp' x)) #opened #(hp_of p) i (reify_steel_ghost_comp f)) in rewrite_slprop (to_vprop (hp_of (fp' x))) (fp' x) (fun _ -> ()); return (hide x)
{ "checked_file": "/", "dependencies": [ "Steel.Semantics.Hoare.MST.fst.checked", "Steel.Memory.fsti.checked", "Steel.Effect.fst.checked", "Steel.Effect.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.NMSTTotal.fst.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "Steel.Effect.Atomic.fst" }
[ { "abbrev": true, "full_module": "FStar.Universe", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "Steel.Semantics.Hoare.MST", "short_module": "Sem" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Common", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics", "short_module": "T" }, { "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": 1, "initial_ifuel": 1, "max_fuel": 1, "max_ifuel": 1, "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": 20, "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.Atomic.SteelGhost Prims.unit
Steel.Effect.Atomic.SteelGhost
[]
[]
[ "Steel.Memory.inames", "Steel.Effect.Common.vprop", "Steel.Effect.Common.normal", "Steel.Effect.Common.t_of", "Prims.prop", "Steel.Effect.Atomic.change_slprop", "Steel.Effect.Common.vrefine", "Steel.Memory.mem", "Steel.Effect.Common.vrefine_sel_eq", "Prims.unit", "Steel.Effect.Common.interp_vrefine_hp", "FStar.Ghost.erased", "Steel.Effect.Common.vrefine_t", "FStar.Ghost.hide", "FStar.Ghost.reveal", "Steel.Effect.Atomic.gget", "Steel.Effect.Common.rmem'", "Steel.Effect.Common.valid_rmem", "Steel.Effect.Atomic.get", "Steel.Effect.Common.rmem" ]
[]
false
true
false
false
false
let intro_vrefine v p =
let m = get () in let x:Ghost.erased (t_of v) = gget v in let x':Ghost.erased (vrefine_t v p) = Ghost.hide (Ghost.reveal x) in change_slprop v (vrefine v p) x x' (fun m -> interp_vrefine_hp v p m; vrefine_sel_eq v p m)
false
Steel.Effect.Atomic.fst
Steel.Effect.Atomic.elim_vrefine
val elim_vrefine (#opened:inames) (v: vprop) (p: (normal (t_of v) -> Tot prop)) : SteelGhost unit opened (vrefine v p) (fun _ -> v) (requires fun _ -> True) (ensures fun h _ h' -> h' v == h (vrefine v p) /\ p (h' v))
val elim_vrefine (#opened:inames) (v: vprop) (p: (normal (t_of v) -> Tot prop)) : SteelGhost unit opened (vrefine v p) (fun _ -> v) (requires fun _ -> True) (ensures fun h _ h' -> h' v == h (vrefine v p) /\ p (h' v))
let elim_vrefine v p = let h = get() in let x : Ghost.erased (vrefine_t v p) = gget (vrefine v p) in let x' : Ghost.erased (t_of v) = Ghost.hide (Ghost.reveal x) in change_slprop (vrefine v p) v x x' (fun m -> interp_vrefine_hp v p m; vrefine_sel_eq v p m )
{ "file_name": "lib/steel/Steel.Effect.Atomic.fst", "git_rev": "f984200f79bdc452374ae994a5ca837496476c41", "git_url": "https://github.com/FStarLang/steel.git", "project_name": "steel" }
{ "end_col": 5, "end_line": 719, "start_col": 0, "start_line": 707 }
(* 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.Atomic open Steel.Effect friend Steel.Effect #set-options "--warn_error -330" //turn off the experimental feature warning let _ : squash (forall (pre:pre_t) (m0:mem{interp (hp_of pre) m0}) (m1:mem{disjoint m0 m1}). mk_rmem pre m0 == mk_rmem pre (join m0 m1)) = Classical.forall_intro rmem_depends_only_on let req_to_act_req (#pre:vprop) (req:req_t pre) : mprop (hp_of pre) = fun m -> rmem_depends_only_on pre; interp (hp_of pre) m /\ req (mk_rmem pre m) unfold let to_post (#a:Type) (post:post_t a) = fun x -> (hp_of (post x)) let ens_to_act_ens (#pre:pre_t) (#a:Type) (#post:post_t a) (ens:ens_t pre a post) : mprop2 (hp_of pre) (to_post post) = fun m0 x m1 -> interp (hp_of pre) m0 /\ interp (hp_of (post x)) m1 /\ ens (mk_rmem pre m0) x (mk_rmem (post x) m1) let repr a framed opened f pre post req ens = action_except_full a opened (hp_of pre) (to_post post) (req_to_act_req req) (ens_to_act_ens ens) let return_ a x opened #p = fun _ -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (p x) (core_mem m0) in lemma_frame_equalities_refl (p x) h0; x #push-options "--fuel 0 --ifuel 0" #push-options "--z3rlimit 20 --fuel 1 --ifuel 1" val frame00 (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:repr a framed opened obs pre post req ens) (frame:vprop) : repr a true opened obs (pre `star` frame) (fun x -> post x `star` frame) (fun h -> req (focus_rmem h pre)) (fun h0 r h1 -> req (focus_rmem h0 pre) /\ ens (focus_rmem h0 pre) r (focus_rmem h1 (post r)) /\ frame_opaque frame (focus_rmem h0 frame) (focus_rmem h1 frame)) module Sem = Steel.Semantics.Hoare.MST module Mem = Steel.Memory let equiv_middle_left_assoc (a b c d:slprop) : Lemma (((a `Mem.star` b) `Mem.star` c `Mem.star` d) `Mem.equiv` (a `Mem.star` (b `Mem.star` c) `Mem.star` d)) = let open Steel.Memory in star_associative a b c; star_congruence ((a `star` b) `star` c) d (a `star` (b `star` c)) d let frame00 #a #framed #opened #obs #pre #post #req #ens f frame = fun frame' -> let m0:full_mem = NMSTTotal.get () in let snap:rmem frame = mk_rmem frame (core_mem m0) in // Need to define it with type annotation, although unused, for it to trigger // the pattern on the framed ensures in the def of MstTot let aux:mprop (hp_of frame `Mem.star` frame') = req_frame frame snap in focus_is_restrict_mk_rmem (pre `star` frame) pre (core_mem m0); assert (interp (hp_of (pre `star` frame) `Mem.star` frame' `Mem.star` locks_invariant opened m0) m0); equiv_middle_left_assoc (hp_of pre) (hp_of frame) frame' (locks_invariant opened m0); assert (interp (hp_of pre `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m0) m0); let x = f (hp_of frame `Mem.star` frame') in let m1:full_mem = NMSTTotal.get () in assert (interp (hp_of (post x) `Mem.star` (hp_of frame `Mem.star` frame') `Mem.star` locks_invariant opened m1) m1); equiv_middle_left_assoc (hp_of (post x)) (hp_of frame) frame' (locks_invariant opened m1); assert (interp ((hp_of (post x) `Mem.star` hp_of frame) `Mem.star` frame' `Mem.star` locks_invariant opened m1) m1); focus_is_restrict_mk_rmem (pre `star` frame) frame (core_mem m0); focus_is_restrict_mk_rmem (post x `star` frame) frame (core_mem m1); let h0:rmem (pre `star` frame) = mk_rmem (pre `star` frame) (core_mem m0) in let h1:rmem (post x `star` frame) = mk_rmem (post x `star` frame) (core_mem m1) in assert (focus_rmem h0 frame == focus_rmem h1 frame); focus_is_restrict_mk_rmem (post x `star` frame) (post x) (core_mem m1); lemma_frame_opaque_refl frame (focus_rmem h0 frame); x unfold let bind_req_opaque (#a:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#pr:a -> prop) (req_g:(x:a -> req_t (pre_g x))) (frame_f:vprop) (frame_g:a -> vprop) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) : req_t (pre_f `star` frame_f) = fun m0 -> req_f (focus_rmem m0 pre_f) /\ (forall (x:a) (h1:hmem (post_f x `star` frame_f)). (ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f)) ==> pr x /\ (can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); (req_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)))) unfold let bind_ens_opaque (#a:Type) (#b:Type) (#pre_f:pre_t) (#post_f:post_t a) (req_f:req_t pre_f) (ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#pr:a -> prop) (ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (frame_f:vprop) (frame_g:a -> vprop) (post:post_t b) (_:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (_:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) : ens_t (pre_f `star` frame_f) b post = fun m0 y m2 -> req_f (focus_rmem m0 pre_f) /\ (exists (x:a) (h1:hmem (post_f x `star` frame_f)). pr x /\ ( can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); frame_opaque frame_f (focus_rmem m0 frame_f) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) frame_f) /\ frame_opaque (frame_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (frame_g x)) (focus_rmem m2 (frame_g x)) /\ ens_f (focus_rmem m0 pre_f) x (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (post_f x)) /\ (ens_g x) (focus_rmem (mk_rmem (post_f x `star` frame_f) h1) (pre_g x)) y (focus_rmem m2 (post_g x y)))) val bind_opaque (a:Type) (b:Type) (opened_invariants:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#pre_f:pre_t) (#post_f:post_t a) (#req_f:req_t pre_f) (#ens_f:ens_t pre_f a post_f) (#pre_g:a -> pre_t) (#post_g:a -> post_t b) (#req_g:(x:a -> req_t (pre_g x))) (#ens_g:(x:a -> ens_t (pre_g x) b (post_g x))) (#frame_f:vprop) (#frame_g:a -> vprop) (#post:post_t b) (# _ : squash (maybe_emp framed_f frame_f)) (# _ : squash (maybe_emp_dep framed_g frame_g)) (#pr:a -> prop) (#p1:squash (can_be_split_forall_dep pr (fun x -> post_f x `star` frame_f) (fun x -> pre_g x `star` frame_g x))) (#p2:squash (can_be_split_post (fun x y -> post_g x y `star` frame_g x) post)) (f:repr a framed_f opened_invariants o1 pre_f post_f req_f ens_f) (g:(x:a -> repr b framed_g opened_invariants o2 (pre_g x) (post_g x) (req_g x) (ens_g x))) : Pure (repr b true opened_invariants (join_obs o1 o2) (pre_f `star` frame_f) post (bind_req_opaque req_f ens_f req_g frame_f frame_g p1) (bind_ens_opaque req_f ens_f ens_g frame_f frame_g post p1 p2) ) (requires obs_at_most_one o1 o2) (ensures fun _ -> True) #push-options "--z3rlimit 20" let bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem (pre_f `star` frame_f) (core_mem m0) in let x = frame00 f frame_f frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_f x `star` frame_f) (core_mem m1) in let h1' = mk_rmem (pre_g x `star` frame_g x) (core_mem m1) in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); focus_is_restrict_mk_rmem (post_f x `star` frame_f) (pre_g x `star` frame_g x) (core_mem m1); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x) (core_mem m1); assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); can_be_split_3_interp (hp_of (post_f x `star` frame_f)) (hp_of (pre_g x `star` frame_g x)) frame (locks_invariant opened m1) m1; let y = frame00 (g x) (frame_g x) frame in let m2:full_mem = NMSTTotal.get () in can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (pre_g x); can_be_split_trans (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x); can_be_split_trans (post y) (post_g x y `star` frame_g x) (post_g x y); can_be_split_trans (post y) (post_g x y `star` frame_g x) (frame_g x); let h2' = mk_rmem (post_g x y `star` frame_g x) (core_mem m2) in let h2 = mk_rmem (post y) (core_mem m2) in // assert (focus_rmem h1' (pre_g x) == focus_rmem h1 (pre_g x)); focus_focus_is_focus (post_f x `star` frame_f) (pre_g x `star` frame_g x) (frame_g x) (core_mem m1); focus_is_restrict_mk_rmem (post_g x y `star` frame_g x) (post y) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (frame_g x) (core_mem m2); focus_focus_is_focus (post_g x y `star` frame_g x) (post y) (post_g x y) (core_mem m2); can_be_split_3_interp (hp_of (post_g x y `star` frame_g x)) (hp_of (post y)) frame (locks_invariant opened m2) m2; y let norm_repr (#a:Type) (#framed:bool) (#opened:inames) (#obs:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) (f:repr a framed opened obs pre post req ens) : repr a framed opened obs pre post (fun h -> norm_opaque (req h)) (fun h0 x h1 -> norm_opaque (ens h0 x h1)) = f let bind a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g = norm_repr (bind_opaque a b opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #frame_f #frame_g #post #_ #_ #p #p2 f g) val subcomp_opaque (a:Type) (opened:inames) (o1:eqtype_as_type observability) (o2:eqtype_as_type observability) (#framed_f:eqtype_as_type bool) (#framed_g:eqtype_as_type bool) (#[@@@ framing_implicit] pre_f:pre_t) (#[@@@ framing_implicit] post_f:post_t a) (#[@@@ framing_implicit] req_f:req_t pre_f) (#[@@@ framing_implicit] ens_f:ens_t pre_f a post_f) (#[@@@ framing_implicit] pre_g:pre_t) (#[@@@ framing_implicit] post_g:post_t a) (#[@@@ framing_implicit] req_g:req_t pre_g) (#[@@@ framing_implicit] ens_g:ens_t pre_g a post_g) (#[@@@ framing_implicit] frame:vprop) (#[@@@ framing_implicit] pr : prop) (#[@@@ framing_implicit] _ : squash (maybe_emp framed_f frame)) (#[@@@ framing_implicit] p1:squash (can_be_split_dep pr pre_g (pre_f `star` frame))) (#[@@@ framing_implicit] p2:squash (equiv_forall post_g (fun x -> post_f x `star` frame))) (f:repr a framed_f opened o1 pre_f post_f req_f ens_f) : Pure (repr a framed_g opened o2 pre_g post_g req_g ens_g) (requires (o1 = Unobservable || o2 = Observable) /\ subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2) (ensures fun _ -> True) let subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem pre_g (core_mem m0) in can_be_split_trans pre_g (pre_f `star` fr) pre_f; can_be_split_trans pre_g (pre_f `star` fr) fr; can_be_split_3_interp (hp_of pre_g) (hp_of (pre_f `star` fr)) frame (locks_invariant opened m0) m0; focus_replace pre_g (pre_f `star` fr) pre_f (core_mem m0); let x = frame00 f fr frame in let m1:full_mem = NMSTTotal.get () in let h1 = mk_rmem (post_g x) (core_mem m1) in let h0' = mk_rmem (pre_f `star` fr) (core_mem m0) in let h1' = mk_rmem (post_f x `star` fr) (core_mem m1) in // From frame00 assert (frame_opaque fr (focus_rmem h0' fr) (focus_rmem h1' fr)); // Replace h0'/h1' by h0/h1 focus_replace pre_g (pre_f `star` fr) fr (core_mem m0); focus_replace (post_g x) (post_f x `star` fr) fr (core_mem m1); assert (frame_opaque fr (focus_rmem h0 fr) (focus_rmem h1 fr)); can_be_split_trans (post_g x) (post_f x `star` fr) (post_f x); can_be_split_trans (post_g x) (post_f x `star` fr) fr; can_be_split_3_interp (hp_of (post_f x `star` fr)) (hp_of (post_g x)) frame (locks_invariant opened m1) m1; focus_replace (post_g x) (post_f x `star` fr) (post_f x) (core_mem m1); x let subcomp a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #_ #pr #p1 #p2 f = lemma_subcomp_pre_opaque req_f ens_f req_g ens_g p1 p2; subcomp_opaque a opened o1 o2 #framed_f #framed_g #pre_f #post_f #req_f #ens_f #pre_g #post_g #req_g #ens_g #fr #pr #_ #p1 #p2 f #pop-options let bind_pure_steela_ a b opened o #wp f g = FStar.Monotonic.Pure.elim_pure_wp_monotonicity wp; fun frame -> let x = f () in g x frame let lift_ghost_atomic a o f = f let lift_atomic_steel a o f = f let as_atomic_action f = SteelAtomic?.reflect f let as_atomic_action_ghost f = SteelGhost?.reflect f let as_atomic_unobservable_action f = SteelAtomicU?.reflect f (* Some helpers *) let get0 (#opened:inames) (#p:vprop) (_:unit) : repr (erased (rmem p)) true opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1 /\ frame_equalities p r h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; h0 let get () = SteelGhost?.reflect (get0 ()) let intro_star (p q:vprop) (r:slprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (m:mem) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m) ) : Lemma (requires interp ((hp_of p) `Mem.star` r) m /\ sel_of p m == reveal vp) (ensures interp ((hp_of q) `Mem.star` r) m) = let p = hp_of p in let q = hp_of q in let intro (ml mr:mem) : Lemma (requires interp q ml /\ interp r mr /\ disjoint ml mr) (ensures disjoint ml mr /\ interp (q `Mem.star` r) (Mem.join ml mr)) = Mem.intro_star q r ml mr in elim_star p r m; Classical.forall_intro (Classical.move_requires proof); Classical.forall_intro_2 (Classical.move_requires_2 intro) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let rewrite_slprop0 (#opened:inames) (p q:vprop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ _ -> True) = fun frame -> let m:full_mem = NMSTTotal.get () in proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) (sel_of q m) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let rewrite_slprop p q l = SteelGhost?.reflect (rewrite_slprop0 p q l) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop0 (#opened:inames) (p q:vprop) (vp:erased (t_of p)) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun h -> h p == reveal vp) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) vp vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop p q vp vq l = SteelGhost?.reflect (change_slprop0 p q vp vq l) let change_equal_slprop p q = let m = get () in let x : Ghost.erased (t_of p) = hide ((reveal m) p) in let y : Ghost.erased (t_of q) = Ghost.hide (Ghost.reveal x) in change_slprop p q x y (fun _ -> ()) #push-options "--z3rlimit 20 --fuel 1 --ifuel 0" let change_slprop_20 (#opened:inames) (p q:vprop) (vq:erased (t_of q)) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of q) m /\ sel_of q m == reveal vq) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun _ _ h1 -> h1 q == reveal vq) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); Classical.forall_intro (Classical.move_requires proof); Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p m) vq m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) #pop-options let change_slprop_2 p q vq l = SteelGhost?.reflect (change_slprop_20 p q vq l) let change_slprop_rel0 (#opened:inames) (p q:vprop) (rel : normal (t_of p) -> normal (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun _ -> True) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel p q rel proof = SteelGhost?.reflect (change_slprop_rel0 p q rel proof) let change_slprop_rel_with_cond0 (#opened:inames) (p q:vprop) (cond: t_of p -> prop) (rel : (t_of p) -> (t_of q) -> prop) (proof:(m:mem) -> Lemma (requires interp (hp_of p) m /\ cond (sel_of p m)) (ensures interp (hp_of p) m /\ interp (hp_of q) m /\ rel (sel_of p m) (sel_of q m)) ) : repr unit false opened Unobservable p (fun _ -> q) (fun m -> cond (m p)) (fun h0 _ h1 -> rel (h0 p) (h1 q)) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; proof (core_mem m); let h0 = mk_rmem p (core_mem m) in let h1 = mk_rmem q (core_mem m) in reveal_mk_rmem p (core_mem m) p; reveal_mk_rmem q (core_mem m) q; Mem.star_associative (hp_of p) frame (locks_invariant opened m); intro_star p q (frame `Mem.star` locks_invariant opened m) (sel_of p (core_mem m)) (sel_of q (core_mem m)) m proof; Mem.star_associative (hp_of q) frame (locks_invariant opened m) let change_slprop_rel_with_cond p q cond rel proof = SteelGhost?.reflect (change_slprop_rel_with_cond0 p q cond rel proof) let extract_info0 (#opened:inames) (p:vprop) (vp:erased (normal (t_of p))) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m /\ sel_of p m == reveal vp) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> h p == reveal vp) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info p vp fact l = SteelGhost?.reflect (extract_info0 p vp fact l) let extract_info_raw0 (#opened:inames) (p:vprop) (fact:prop) (l:(m:mem) -> Lemma (requires interp (hp_of p) m) (ensures fact) ) : repr unit false opened Unobservable p (fun _ -> p) (fun h -> True) (fun h0 _ h1 -> frame_equalities p h0 h1 /\ fact) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0; l (core_mem m0) let extract_info_raw p fact l = SteelGhost?.reflect (extract_info_raw0 p fact l) let noop _ = change_slprop_rel emp emp (fun _ _ -> True) (fun _ -> ()) let sladmit _ = SteelGhostF?.reflect (fun _ -> NMSTTotal.nmst_tot_admit ()) let slassert0 (#opened:inames) (p:vprop) : repr unit false opened Unobservable p (fun _ -> p) (requires fun _ -> True) (ensures fun h0 r h1 -> frame_equalities p h0 h1) = fun frame -> let m0:full_mem = NMSTTotal.get () in let h0 = mk_rmem p (core_mem m0) in lemma_frame_equalities_refl p h0 let slassert p = SteelGhost?.reflect (slassert0 p) let drop p = rewrite_slprop p emp (fun m -> emp_unit (hp_of p); affine_star (hp_of p) Mem.emp m; reveal_emp()) let reveal_star0 (#opened:inames) (p1 p2:vprop) : repr unit false opened Unobservable (p1 `star` p2) (fun _ -> p1 `star` p2) (fun _ -> True) (fun h0 _ h1 -> h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 (p1 `star` p2) == (h0 p1, h0 p2) /\ h1 (p1 `star` p2) == (h1 p1, h1 p2) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem let reveal_star p1 p2 = SteelGhost?.reflect (reveal_star0 p1 p2) let reveal_star_30 (#opened:inames) (p1 p2 p3:vprop) : repr unit false opened Unobservable (p1 `star` p2 `star` p3) (fun _ -> p1 `star` p2 `star` p3) (requires fun _ -> True) (ensures fun h0 _ h1 -> can_be_split (p1 `star` p2 `star` p3) p1 /\ can_be_split (p1 `star` p2 `star` p3) p2 /\ h0 p1 == h1 p1 /\ h0 p2 == h1 p2 /\ h0 p3 == h1 p3 /\ h0 (p1 `star` p2 `star` p3) == ((h0 p1, h0 p2), h0 p3) /\ h1 (p1 `star` p2 `star` p3) == ((h1 p1, h1 p2), h1 p3) ) = fun frame -> let m:full_mem = NMSTTotal.get () in Classical.forall_intro_3 reveal_mk_rmem; let h0 = mk_rmem (p1 `star` p2 `star` p3) (core_mem m) in can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p1; can_be_split_trans (p1 `star` p2 `star` p3) (p1 `star` p2) p2; reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2 `star` p3); reveal_mk_rmem (p1 `star` p2 `star` p3) m (p1 `star` p2); reveal_mk_rmem (p1 `star` p2 `star` p3) m p3 let reveal_star_3 p1 p2 p3 = SteelGhost?.reflect (reveal_star_30 p1 p2 p3) let intro_pure p = rewrite_slprop emp (pure p) (fun m -> pure_interp p m) let elim_pure_aux #uses (p:prop) : SteelGhostT (_:unit{p}) uses (pure p) (fun _ -> to_vprop Mem.emp) = as_atomic_action_ghost (Steel.Memory.elim_pure #uses p) let elim_pure #uses p = let _ = elim_pure_aux p in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()) let return #a #opened #p x = SteelAtomicBase?.reflect (return_ a x opened #p) let intro_exists #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists x (h_exists_sl' p) m) let intro_exists_erased #a #opened x p = rewrite_slprop (p x) (h_exists p) (fun m -> Steel.Memory.intro_h_exists (Ghost.reveal x) (h_exists_sl' p) m) let witness_exists #a #u #p _ = SteelGhost?.reflect (Steel.Memory.witness_h_exists #u (fun x -> hp_of (p x))) let lift_exists #a #u p = as_atomic_action_ghost (Steel.Memory.lift_h_exists #u (fun x -> hp_of (p x))) let exists_equiv p q = Classical.forall_intro_2 reveal_equiv; h_exists_cong (h_exists_sl' p) (h_exists_sl' q) let exists_cong p q = rewrite_slprop (h_exists p) (h_exists q) (fun m -> reveal_equiv (h_exists p) (h_exists q); exists_equiv p q) let fresh_invariant #uses p ctxt = rewrite_slprop p (to_vprop (hp_of p)) (fun _ -> ()); let i = as_atomic_unobservable_action (fresh_invariant uses (hp_of p) ctxt) in rewrite_slprop (to_vprop Mem.emp) emp (fun _ -> reveal_emp ()); return i let new_invariant #uses p = let i = fresh_invariant #uses p [] in return i (* * AR: SteelAtomic and SteelGhost are not marked reifiable since we intend to run Steel programs natively * However to implement the with_inv combinators we need to reify their thunks to reprs * We could implement it better by having support for reification only in the .fst file * But for now assuming a function *) assume val reify_steel_atomic_comp (#a:Type) (#framed:bool) (#opened_invariants:inames) (#g:observability) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:unit -> SteelAtomicBase a framed opened_invariants g pre post req ens) : repr a framed opened_invariants g pre post req ens [@@warn_on_use "as_unobservable_atomic_action is a trusted primitive"] let as_atomic_o_action (#a:Type u#a) (#opened_invariants:inames) (#fp:slprop) (#fp': a -> slprop) (o:observability) (f:action_except a opened_invariants fp fp') : SteelAtomicBaseT a opened_invariants o (to_vprop fp) (fun x -> to_vprop (fp' x)) = SteelAtomicBaseT?.reflect f let with_invariant #a #fp #fp' #obs #opened #p i f = rewrite_slprop fp (to_vprop (hp_of fp)) (fun _ -> ()); let x = as_atomic_o_action obs (Steel.Memory.with_invariant #a #(hp_of fp) #(fun x -> hp_of (fp' x)) #opened #(hp_of p) i (reify_steel_atomic_comp f)) in rewrite_slprop (to_vprop (hp_of (fp' x))) (fp' x) (fun _ -> ()); return x assume val reify_steel_ghost_comp (#a:Type) (#framed:bool) (#opened_invariants:inames) (#pre:pre_t) (#post:post_t a) (#req:req_t pre) (#ens:ens_t pre a post) ($f:unit -> SteelGhostBase a framed opened_invariants Unobservable pre post req ens) : repr a framed opened_invariants Unobservable pre post req ens let with_invariant_g #a #fp #fp' #opened #p i f = rewrite_slprop fp (to_vprop (hp_of fp)) (fun _ -> ()); let x = as_atomic_unobservable_action (Steel.Memory.with_invariant #a #(hp_of fp) #(fun x -> hp_of (fp' x)) #opened #(hp_of p) i (reify_steel_ghost_comp f)) in rewrite_slprop (to_vprop (hp_of (fp' x))) (fp' x) (fun _ -> ()); return (hide x) let intro_vrefine v p = let m = get () in let x : Ghost.erased (t_of v) = gget v in let x' : Ghost.erased (vrefine_t v p) = Ghost.hide (Ghost.reveal x) in change_slprop v (vrefine v p) x x' (fun m -> interp_vrefine_hp v p m; vrefine_sel_eq v p m )
{ "checked_file": "/", "dependencies": [ "Steel.Semantics.Hoare.MST.fst.checked", "Steel.Memory.fsti.checked", "Steel.Effect.fst.checked", "Steel.Effect.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.NMSTTotal.fst.checked", "FStar.Monotonic.Pure.fst.checked", "FStar.Ghost.fsti.checked", "FStar.Classical.fsti.checked" ], "interface_file": true, "source_file": "Steel.Effect.Atomic.fst" }
[ { "abbrev": true, "full_module": "FStar.Universe", "short_module": "U" }, { "abbrev": false, "full_module": "FStar.Ghost", "short_module": null }, { "abbrev": true, "full_module": "Steel.Memory", "short_module": "Mem" }, { "abbrev": true, "full_module": "Steel.Semantics.Hoare.MST", "short_module": "Sem" }, { "abbrev": false, "full_module": "Steel.Effect", "short_module": null }, { "abbrev": false, "full_module": "Steel.Effect.Common", "short_module": null }, { "abbrev": true, "full_module": "FStar.Tactics", "short_module": "T" }, { "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": 1, "initial_ifuel": 1, "max_fuel": 1, "max_ifuel": 1, "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": 20, "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.Atomic.SteelGhost Prims.unit
Steel.Effect.Atomic.SteelGhost
[]
[]
[ "Steel.Memory.inames", "Steel.Effect.Common.vprop", "Steel.Effect.Common.normal", "Steel.Effect.Common.t_of", "Prims.prop", "Steel.Effect.Atomic.change_slprop", "Steel.Effect.Common.vrefine", "Steel.Memory.mem", "Steel.Effect.Common.vrefine_sel_eq", "Prims.unit", "Steel.Effect.Common.interp_vrefine_hp", "FStar.Ghost.erased", "FStar.Ghost.hide", "FStar.Ghost.reveal", "Steel.Effect.Common.vrefine_t", "Steel.Effect.Atomic.gget", "Steel.Effect.Common.rmem'", "Steel.Effect.Common.valid_rmem", "Steel.Effect.Atomic.get", "Steel.Effect.Common.rmem" ]
[]
false
true
false
false
false
let elim_vrefine v p =
let h = get () in let x:Ghost.erased (vrefine_t v p) = gget (vrefine v p) in let x':Ghost.erased (t_of v) = Ghost.hide (Ghost.reveal x) in change_slprop (vrefine v p) v x x' (fun m -> interp_vrefine_hp v p m; vrefine_sel_eq v p m)
false
Vale.X64.Print_Inline_s.fst
Vale.X64.Print_Inline_s.print_rettype
val print_rettype : ret_val: FStar.Pervasives.Native.option Prims.string -> Prims.string
let print_rettype (ret_val:option string) = match ret_val with | None -> "void" | Some _ -> "uint64_t"
{ "file_name": "vale/specs/hardware/Vale.X64.Print_Inline_s.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 24, "end_line": 16, "start_col": 0, "start_line": 14 }
module Vale.X64.Print_Inline_s open FStar.Mul open FStar.List.Tot open Vale.X64.Machine_s open Vale.X64.Bytes_Code_s open Vale.X64.Machine_Semantics_s open Vale.X64.Instruction_s open FStar.IO open Vale.Interop.Base open Vale.Interop.X64 module P = Vale.X64.Print_s
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Interop.X64.fsti.checked", "Vale.Interop.Base.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.IO.fst.checked", "FStar.All.fst.checked" ], "interface_file": false, "source_file": "Vale.X64.Print_Inline_s.fst" }
[ { "abbrev": true, "full_module": "Vale.X64.Print_s", "short_module": "P" }, { "abbrev": false, "full_module": "Vale.Interop.X64", "short_module": null }, { "abbrev": false, "full_module": "Vale.Interop.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.IO", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "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": 0, "max_fuel": 1, "max_ifuel": 1, "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": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
ret_val: FStar.Pervasives.Native.option Prims.string -> Prims.string
Prims.Tot
[ "total" ]
[]
[ "FStar.Pervasives.Native.option", "Prims.string" ]
[]
false
false
false
true
false
let print_rettype (ret_val: option string) =
match ret_val with | None -> "void" | Some _ -> "uint64_t"
false
Vale.X64.Print_Inline_s.fst
Vale.X64.Print_Inline_s.print_explicit_register_arg
val print_explicit_register_arg : n: Prims.nat -> a: Vale.Interop.Base.td -> i: Prims.nat{i < n} -> of_arg: (_: Vale.Interop.X64.reg_nat n -> Vale.X64.Machine_s.reg_64) -> reserved: (_: Vale.X64.Machine_s.reg_64 -> Prims.bool) -> names: (_: Prims.nat -> Prims.string) -> Prims.bool * Prims.string
let print_explicit_register_arg (n:nat) (a:td) (i:nat{i < n}) (of_arg:reg_nat n -> reg_64) (reserved:reg_64 -> bool) (names:nat -> string) = let ty = match a with | TD_Base _ -> "uint64_t " | _ -> "uint64_t *" in if reserved (of_arg i) then // If the associated register is reserved, we really this argument in it. For instance if it is Rdx and we have Mul(x) instructions true, " register " ^ ty ^ names i ^ "_r __asm__(\"" ^ P.print_reg_name (of_arg i) ^ "\") = " ^ names i ^ ";\n" else false, ""
{ "file_name": "vale/specs/hardware/Vale.X64.Print_Inline_s.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 16, "end_line": 215, "start_col": 0, "start_line": 207 }
module Vale.X64.Print_Inline_s open FStar.Mul open FStar.List.Tot open Vale.X64.Machine_s open Vale.X64.Bytes_Code_s open Vale.X64.Machine_Semantics_s open Vale.X64.Instruction_s open FStar.IO open Vale.Interop.Base open Vale.Interop.X64 module P = Vale.X64.Print_s let print_rettype (ret_val:option string) = match ret_val with | None -> "void" | Some _ -> "uint64_t" let print_basetype (t:base_typ) = match t with | TUInt8 -> "uint8_t" | TUInt16 -> "uint16_t" | TUInt32 -> "uint32_t" | TUInt64 -> "uint64_t" | TUInt128 -> "ERROR" // Returns "uint8_t arg2" or "uint64_t* arg0" for instance let print_arg (a:td) (i:nat) (names:nat -> string) = match a with | TD_Base src -> print_basetype src ^ " " ^ names i | TD_Buffer src _ _ | TD_ImmBuffer src _ _ -> print_basetype src ^ " *" ^ names i // Prints a list of args with their types, separated by a comma let rec print_args (args:list td) (i:nat) (names:nat -> string) = match args with | [] -> "" | [a] -> print_arg a i names | a::q -> print_arg a i names ^ ", " ^ print_args q (i+1) names let rec build_reserved_args_outs (l:list instr_out) (reserved:reg_64 -> bool) : Tot (reg_64 -> bool) (decreases l) = fun r -> match l with | [] -> reserved r | hd::tl -> let (_, op) = hd in let reserved : (reg_64 -> bool) = (fun r -> match op with | IOpIm (IOp64One (OReg reg)) -> // Implicit register, adding it to "reserved" registers if r = reg then true else reserved r | _ -> reserved r) in build_reserved_args_outs tl reserved r let rec build_reserved_args_ins (l:list instr_operand) (reserved:reg_64 -> bool) : Tot (reg_64 -> bool) (decreases l) = fun r -> match l with | [] -> reserved r | hd::tl -> let reserved : (reg_64 -> bool) = (fun r -> match hd with | IOpIm (IOp64One (OReg reg)) -> // Implicit register, adding it to "reserved" registers if r = reg then true else reserved r | _ -> reserved r) in build_reserved_args_ins tl reserved r // Traverses code, looking for instructions implicitly using registers. // When found, mark such registers as reserved so that they are not used during implicit allocation let rec build_reserved_args (c:code) (reserved:reg_64 -> bool) : Tot (reg_64 -> bool) (decreases c) = fun r -> (match c with | Ins ins -> begin match ins with | Instr i _ _ -> let reserved = build_reserved_args_outs i.outs reserved in let reserved = build_reserved_args_ins (InstrTypeRecord?.args i) reserved in reserved r | _ -> reserved r end | Block l -> (build_reserved_args_block l reserved) r | IfElse cond ifTrue ifFalse -> let reservedT = build_reserved_args ifTrue reserved in build_reserved_args ifFalse reservedT r | While cond body -> build_reserved_args body reserved r ) and build_reserved_args_block (l:list code) (reserved:reg_64 -> bool) : Tot (reg_64 -> bool) (decreases l) = fun r -> ( match l with | [] -> reserved r | hd::tl -> let reserved = build_reserved_args hd reserved in build_reserved_args_block tl reserved r ) // Prints `"=&r" (name)` if an output is specified let print_output_ret ret_val (reg_names:reg_64 -> string) (counter:nat) : list string & (reg_64 -> string) & nat = match ret_val with | None -> [], reg_names, counter | Some name -> (["\"=&r\" (" ^ name ^ ")"], // If r = rax then address it as current arg number (fun r -> if r = 0 then string_of_int counter else reg_names r), counter + 1) // If the register in which a is passed is modified, we should specify `"+&r" (name)` let print_modified_input (n:nat) (a:td) (i:nat{i < n}) (of_arg:reg_nat n -> reg_64) (regs_mod:reg_64 -> bool) (reserved_regs:reg_64 -> bool) (reg_names:reg_64 -> string) (counter:nat) (arg_names:nat -> string) : list string & (reg_64 -> string) & nat = if regs_mod (of_arg i) then (["\"+&r\" (" ^ arg_names i ^ (if reserved_regs (of_arg i) then "_r)" else ")")], (fun r -> if r = of_arg i && not (reserved_regs r) then string_of_int counter else reg_names r), counter + 1 ) else ([], reg_names, counter) // Get a list of strings corresponding to modified inputs let rec get_modified_input_strings (n:nat) (of_arg:reg_nat n -> reg_64) (regs_mod:reg_64 -> bool) (reserved_regs:reg_64 -> bool) (args:list td) (i:nat{i + List.Tot.length args <= n}) (ret_val:option string) (reg_names:reg_64 -> string) (counter:nat) (arg_names:nat -> string) : list string & (reg_64 -> string) & nat = match args with | [] -> print_output_ret ret_val reg_names counter | a::q -> let output, reg_names, counter = print_modified_input n a i of_arg regs_mod reserved_regs reg_names counter arg_names in let outputs, reg_names, counter = get_modified_input_strings n of_arg regs_mod reserved_regs q (i+1) ret_val reg_names counter arg_names in output @ outputs, reg_names, counter // Print the list of modified inputs, separated by commas let print_modified_inputs (n:nat) (of_arg:reg_nat n -> reg_64) (regs_mod:reg_64 -> bool) (reserved_regs:reg_64 -> bool) (args:list td{List.Tot.length args <= n}) (ret_val:option string) (reg_names:reg_64 -> string) (counter:nat) (arg_names:nat -> string) = let rec aux = function | [] -> "\n" | [a] -> a ^ "\n" | a :: q -> a ^ ", " ^ aux q in let outputs, output_reg_names, output_nbr = get_modified_input_strings n of_arg regs_mod reserved_regs args 0 ret_val reg_names counter arg_names in aux outputs, output_reg_names, output_nbr // If the register in which an arg is passed is not modified, we should specify it as `"r" (name)` let print_nonmodified_input (n:nat) (of_arg:reg_nat n -> reg_64) (regs_mod:reg_64 -> bool) (reserved_regs:reg_64 -> bool) (a:td) (i:nat{i < n}) (reg_names:reg_64 -> string) (counter:nat) (arg_names:nat -> string) : list string & (reg_64 -> string) & nat = if regs_mod (of_arg i) then ([], reg_names, counter) else (["\"r\" (" ^ arg_names i ^ (if reserved_regs (of_arg i) then "_r)" else ")")], (fun r -> if r = of_arg i && not (reserved_regs r) then string_of_int counter else reg_names r), counter + 1) // Get a list of strings corresponding to modified inputs let rec get_nonmodified_input_strings (n:nat) (of_arg:reg_nat n -> reg_64) (regs_mod:reg_64 -> bool) (reserved_regs:reg_64 -> bool) (args:list td) (i:nat{List.Tot.length args + i <= n}) (reg_names:reg_64 -> string) (counter:nat) (arg_names:nat -> string) = match args with | [] -> [], reg_names, counter | a::q -> let input, reg_names, counter = print_nonmodified_input n of_arg regs_mod reserved_regs a i reg_names counter arg_names in let inputs, reg_names, counter = get_nonmodified_input_strings n of_arg regs_mod reserved_regs q (i+1) reg_names counter arg_names in input @ inputs, reg_names, counter let print_nonmodified_inputs (n:nat) (of_arg:reg_nat n -> reg_64) (regs_mod:reg_64 -> bool) (reserved_regs:reg_64 -> bool) (args:list td{List.Tot.length args <= n}) (reg_names:reg_64 -> string) (counter:nat) (arg_names:nat -> string) = let rec aux = function | [] -> "\n" | [a] -> a ^ "\n" | a :: q -> a ^ ", " ^ aux q in let inputs, input_reg_names, counter = get_nonmodified_input_strings n of_arg regs_mod reserved_regs args 0 reg_names counter arg_names in aux inputs, input_reg_names, counter // Print the list of modified registers, + memory and cc let print_modified_registers (n:nat) (ret_val:option string) (of_arg:reg_nat n -> reg_64) (regs_mod:reg_64 -> bool) (reserved_args:reg_64 -> bool) (args:list td) = // This register was already specified as output let output_register a = Some? ret_val && a = rRax in let rec input_register (i:nat) (a:reg_64) : Tot bool (decreases (n-i)) = if i >= n then false else a = of_arg i // This register was already specified for the i-th argument || input_register (i+1) a in let rec aux = function | [] -> "\"memory\", \"cc\"\n" | a::q -> // This register is not modified, or was already specified as input or output: we skip it if not (regs_mod a) || input_register 0 a || output_register a then aux q // Register not modified or already specified in inputs, we add it else "\"%" ^ P.print_reg_name a ^ "\", " ^ aux q in aux [rRax; rRbx; rRcx; rRdx; rRsi; rRdi; rRbp; rRsp; rR8; rR9; rR10; rR11; rR12; rR13; rR14; rR15]
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Interop.X64.fsti.checked", "Vale.Interop.Base.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.IO.fst.checked", "FStar.All.fst.checked" ], "interface_file": false, "source_file": "Vale.X64.Print_Inline_s.fst" }
[ { "abbrev": true, "full_module": "Vale.X64.Print_s", "short_module": "P" }, { "abbrev": false, "full_module": "Vale.Interop.X64", "short_module": null }, { "abbrev": false, "full_module": "Vale.Interop.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.IO", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "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": 0, "max_fuel": 1, "max_ifuel": 1, "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": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
n: Prims.nat -> a: Vale.Interop.Base.td -> i: Prims.nat{i < n} -> of_arg: (_: Vale.Interop.X64.reg_nat n -> Vale.X64.Machine_s.reg_64) -> reserved: (_: Vale.X64.Machine_s.reg_64 -> Prims.bool) -> names: (_: Prims.nat -> Prims.string) -> Prims.bool * Prims.string
Prims.Tot
[ "total" ]
[]
[ "Prims.nat", "Vale.Interop.Base.td", "Prims.b2t", "Prims.op_LessThan", "Vale.Interop.X64.reg_nat", "Vale.X64.Machine_s.reg_64", "Prims.bool", "Prims.string", "FStar.Pervasives.Native.Mktuple2", "Prims.op_Hat", "Vale.X64.Print_s.print_reg_name", "FStar.Pervasives.Native.tuple2", "Vale.Interop.Base.valid_base_type" ]
[]
false
false
false
false
false
let print_explicit_register_arg (n: nat) (a: td) (i: nat{i < n}) (of_arg: (reg_nat n -> reg_64)) (reserved: (reg_64 -> bool)) (names: (nat -> string)) =
let ty = match a with | TD_Base _ -> "uint64_t " | _ -> "uint64_t *" in if reserved (of_arg i) then true, " register " ^ ty ^ names i ^ "_r __asm__(\"" ^ P.print_reg_name (of_arg i) ^ "\") = " ^ names i ^ ";\n" else false, ""
false
Vale.X64.Print_Inline_s.fst
Vale.X64.Print_Inline_s.print_arg
val print_arg : a: Vale.Interop.Base.td -> i: Prims.nat -> names: (_: Prims.nat -> Prims.string) -> Prims.string
let print_arg (a:td) (i:nat) (names:nat -> string) = match a with | TD_Base src -> print_basetype src ^ " " ^ names i | TD_Buffer src _ _ | TD_ImmBuffer src _ _ -> print_basetype src ^ " *" ^ names i
{ "file_name": "vale/specs/hardware/Vale.X64.Print_Inline_s.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 83, "end_line": 28, "start_col": 0, "start_line": 26 }
module Vale.X64.Print_Inline_s open FStar.Mul open FStar.List.Tot open Vale.X64.Machine_s open Vale.X64.Bytes_Code_s open Vale.X64.Machine_Semantics_s open Vale.X64.Instruction_s open FStar.IO open Vale.Interop.Base open Vale.Interop.X64 module P = Vale.X64.Print_s let print_rettype (ret_val:option string) = match ret_val with | None -> "void" | Some _ -> "uint64_t" let print_basetype (t:base_typ) = match t with | TUInt8 -> "uint8_t" | TUInt16 -> "uint16_t" | TUInt32 -> "uint32_t" | TUInt64 -> "uint64_t" | TUInt128 -> "ERROR"
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Interop.X64.fsti.checked", "Vale.Interop.Base.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.IO.fst.checked", "FStar.All.fst.checked" ], "interface_file": false, "source_file": "Vale.X64.Print_Inline_s.fst" }
[ { "abbrev": true, "full_module": "Vale.X64.Print_s", "short_module": "P" }, { "abbrev": false, "full_module": "Vale.Interop.X64", "short_module": null }, { "abbrev": false, "full_module": "Vale.Interop.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.IO", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "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": 0, "max_fuel": 1, "max_ifuel": 1, "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": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
a: Vale.Interop.Base.td -> i: Prims.nat -> names: (_: Prims.nat -> Prims.string) -> Prims.string
Prims.Tot
[ "total" ]
[]
[ "Vale.Interop.Base.td", "Prims.nat", "Prims.string", "Vale.Interop.Base.valid_base_type", "Prims.op_Hat", "Vale.X64.Print_Inline_s.print_basetype", "Vale.Arch.HeapTypes_s.base_typ", "Vale.Interop.Base.buffer_qualifiers" ]
[]
false
false
false
true
false
let print_arg (a: td) (i: nat) (names: (nat -> string)) =
match a with | TD_Base src -> print_basetype src ^ " " ^ names i | TD_Buffer src _ _ | TD_ImmBuffer src _ _ -> print_basetype src ^ " *" ^ names i
false
Vale.X64.Print_Inline_s.fst
Vale.X64.Print_Inline_s.print_args
val print_args : args: Prims.list Vale.Interop.Base.td -> i: Prims.nat -> names: (_: Prims.nat -> Prims.string) -> Prims.string
let rec print_args (args:list td) (i:nat) (names:nat -> string) = match args with | [] -> "" | [a] -> print_arg a i names | a::q -> print_arg a i names ^ ", " ^ print_args q (i+1) names
{ "file_name": "vale/specs/hardware/Vale.X64.Print_Inline_s.fst", "git_rev": "eb1badfa34c70b0bbe0fe24fe0f49fb1295c7872", "git_url": "https://github.com/project-everest/hacl-star.git", "project_name": "hacl-star" }
{ "end_col": 65, "end_line": 34, "start_col": 0, "start_line": 31 }
module Vale.X64.Print_Inline_s open FStar.Mul open FStar.List.Tot open Vale.X64.Machine_s open Vale.X64.Bytes_Code_s open Vale.X64.Machine_Semantics_s open Vale.X64.Instruction_s open FStar.IO open Vale.Interop.Base open Vale.Interop.X64 module P = Vale.X64.Print_s let print_rettype (ret_val:option string) = match ret_val with | None -> "void" | Some _ -> "uint64_t" let print_basetype (t:base_typ) = match t with | TUInt8 -> "uint8_t" | TUInt16 -> "uint16_t" | TUInt32 -> "uint32_t" | TUInt64 -> "uint64_t" | TUInt128 -> "ERROR" // Returns "uint8_t arg2" or "uint64_t* arg0" for instance let print_arg (a:td) (i:nat) (names:nat -> string) = match a with | TD_Base src -> print_basetype src ^ " " ^ names i | TD_Buffer src _ _ | TD_ImmBuffer src _ _ -> print_basetype src ^ " *" ^ names i
{ "checked_file": "/", "dependencies": [ "Vale.X64.Print_s.fst.checked", "Vale.X64.Machine_Semantics_s.fst.checked", "Vale.X64.Machine_s.fst.checked", "Vale.X64.Instruction_s.fsti.checked", "Vale.X64.Bytes_Code_s.fst.checked", "Vale.Interop.X64.fsti.checked", "Vale.Interop.Base.fst.checked", "prims.fst.checked", "FStar.Pervasives.Native.fst.checked", "FStar.Pervasives.fsti.checked", "FStar.Mul.fst.checked", "FStar.List.Tot.fst.checked", "FStar.List.fst.checked", "FStar.IO.fst.checked", "FStar.All.fst.checked" ], "interface_file": false, "source_file": "Vale.X64.Print_Inline_s.fst" }
[ { "abbrev": true, "full_module": "Vale.X64.Print_s", "short_module": "P" }, { "abbrev": false, "full_module": "Vale.Interop.X64", "short_module": null }, { "abbrev": false, "full_module": "Vale.Interop.Base", "short_module": null }, { "abbrev": false, "full_module": "FStar.IO", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Instruction_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_Semantics_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Bytes_Code_s", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64.Machine_s", "short_module": null }, { "abbrev": false, "full_module": "FStar.List.Tot", "short_module": null }, { "abbrev": false, "full_module": "FStar.Mul", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "short_module": null }, { "abbrev": false, "full_module": "Vale.X64", "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": 0, "max_fuel": 1, "max_ifuel": 1, "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": true, "smtencoding_l_arith_repr": "native", "smtencoding_nl_arith_repr": "wrapped", "smtencoding_valid_elim": false, "smtencoding_valid_intro": true, "tcnorm": true, "trivial_pre_for_unannotated_effectful_fns": false, "z3cliopt": [ "smt.arith.nl=false", "smt.QI.EAGER_THRESHOLD=100", "smt.CASE_SPLIT=3" ], "z3refresh": false, "z3rlimit": 5, "z3rlimit_factor": 1, "z3seed": 0, "z3smtopt": [], "z3version": "4.8.5" }
false
args: Prims.list Vale.Interop.Base.td -> i: Prims.nat -> names: (_: Prims.nat -> Prims.string) -> Prims.string
Prims.Tot
[ "total" ]
[]
[ "Prims.list", "Vale.Interop.Base.td", "Prims.nat", "Prims.string", "Vale.X64.Print_Inline_s.print_arg", "Prims.op_Hat", "Vale.X64.Print_Inline_s.print_args", "Prims.op_Addition" ]
[ "recursion" ]
false
false
false
true
false
let rec print_args (args: list td) (i: nat) (names: (nat -> string)) =
match args with | [] -> "" | [a] -> print_arg a i names | a :: q -> print_arg a i names ^ ", " ^ print_args q (i + 1) names
false